【BZOJ1844/2210】Pku1379 Run Away 模拟退火

【BZOJ1844/2210】Pku1379 Run Away

题意:矩形区域中有一堆点,求矩形中一个位置使得它到所有点的距离的最小值最大。

题解:模拟退火的裸题,再调调调调调参就行了~

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
const int maxn=1010;
struct pdd
{
	double x,y;
	pdd() {}
	pdd(double a,double b) {x=a,y=b;}
}p[maxn];
int n;
double X,Y,T,mx;
pdd ans,now,neo;
double getdis(pdd a,pdd b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int rd()
{
	int ret=0,f=1;	char gc=getchar();
	while(gc<‘0‘||gc>‘9‘)	{if(gc==‘-‘)f=-f;	gc=getchar();}
	while(gc>=‘0‘&&gc<=‘9‘)	ret=ret*10+gc-‘0‘,gc=getchar();
	return ret*f;
}
double solve(pdd a)
{
	double ret=1e10;
	for(int i=1;i<=n;i++)	ret=min(ret,getdis(a,p[i]));
	if(ret>mx)	mx=ret,ans=a;
	return ret;
}
double Rand()
{
	return rand()%1000/1000.0;
}
void work()
{
	X=rd(),Y=rd(),n=rd();
	int i,j;
	for(i=1;i<=n;i++)	p[i].x=rd(),p[i].y=rd();
	mx=0;
	for(j=1;j<=50;j++)
	{
		now.x=Rand()*X,now.y=Rand()*Y,solve(now),T=1000;
		while(T>1e-3)
		{
			double a=2.0*acos(-1.0)*Rand();
			neo.x=now.x+T*cos(a),neo.y=now.y+T*sin(a),T*=0.95;
			if(neo.x<0||neo.x>X||neo.y<0||neo.y>Y)	continue;
			double de=solve(neo)-solve(now);
			if(de>0)	now=neo;
		}
		T=0.5;
		for(i=1;i<=500;i++)
		{
			double a=2.0*acos(-1.0)*Rand();
			neo.x=now.x+T*cos(a);
			neo.y=now.y+T*sin(a);
			if(neo.x<0||neo.x>X||neo.y<0||neo.y>Y)	continue;
			solve(neo);
		}
	}
	printf("The safest point is (%.1lf, %.1lf).\n",ans.x,ans.y);
}
int main()
{
	srand(2333666);
	int T=rd();
	while(T--)	work();
}
时间: 2024-08-29 01:03:52

【BZOJ1844/2210】Pku1379 Run Away 模拟退火的相关文章

Run Away 模拟退火

Run Away Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look

PKU 1379 Run Away(模拟退火算法)

题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用模拟退火算法解决. 不过个人感觉这个算法的实现过程中仍然采用了随机化算法.二者均属于概率算法.  参考链接 Point Goto_Rand_Dir(double key,Point temp)函数中,Point temp必须得定义在参数中,不能定义在函数内部, 否则temp没有初始值,无法进行后面的

POJ 1379 Run Away 模拟退火

一开始写了一发很快的,发现一会能过一会不能,貌似有点悬,毕竟是随机算法. 后来重写了一发迭代5遍的,基本上把把AC了= = 模拟退火果然是一种不是很靠谱的算法. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; const

poj 1379 Run Away 模拟退火 难度:1

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6482   Accepted: 1993 Description One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look complet

【POJ1379】Run Away 模拟退火

#include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/43526827"); } 题意: 给若干个点,现在求一个点,使它到离它最近的点尽量远. 题解: 我写的是模拟退火先玩一会,然后小幅度爬爬山. 种子的采用是20134858 是生日^人的名字首字母hash. 诶可算A了,看来我脸还不是太黑. 代

【模拟退火】poj1379 Run Away

题意:平面上找一个点,使得其到给定的n个点的距离的最小值最大. 模拟退火看这篇:http://www.cnblogs.com/autsky-jadek/p/7524208.html 这题稍有不同之处仅有:多随机几个初始点,以增加正确率. 另:WA了几百遍竟然是因为最后输出了-0.0这样的值…… #include<cstdio> #include<cmath> #include<algorithm> #include<cstdlib> using namesp

POJ 1379 Run Away 【基础模拟退火】

题意:找出一点,距离所有所有点的最短距离最大 二维平面内模拟退火即可,同样这题用最小圆覆盖也是可以的. Source Code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cm

poj-2420 A Star not a Tree?(模拟退火算法)

题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepted: 2491 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allo

docker run常用命令及 解决 ubuntu镜像无法识别 ifconfig ping 命令

docker run -it     docker 前端启动 container容器           -d             后端启动 container容器           -p             固定端口映射            -P             不固定端口映射           --name         给生成的容器起名字docker ps:默认显示正在运行的container       ps -a 显示所有的container容器docker r