模拟退火。
随机出30个点,模拟退火,跳的步长随温度降低以保证稳定,每次挑30个点来跳,接下来就看RP了
不知道有没有能证明正确性的正解,感觉模拟退火挺不靠谱的- -
自己人品比较差,降温时间弄得比较长,果然AC了
#include<ctime>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define NUM 30
using namespace std;
const double INF = 1e20, eps = 1e-8, pi = acos(-1.0);
double rx[1005], ry[1005], px[33], py[33], d[33];
double dis(double x1,double y1,double x2,double y2)
{return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}
int main()
{
int T;
scanf("%d",&T);
srand(time(0));
while(T--)
{
int n;
double x,y;
scanf("%lf%lf%d",&x,&y,&n);
for(int i = 1; i <= n; i++)
scanf("%lf%lf",&rx[i],&ry[i]);
for(int i = 1; i <= NUM; i++)
{
px[i]=double(rand()%1000+1)/1000*x;
py[i]=double(rand()%1000+1)/1000*y;
d[i]=INF;
for(int j = 1; j <= n; j++)
d[i]=min(d[i],dis(px[i],py[i],rx[j],ry[j]));
}
double delta=sqrt(x*x+y*y)*0.5;
while(delta>eps)
{
for(int i = 1; i <= NUM; i++)
{
double qx=px[i], qy=py[i];
for(int j = 1; j <= NUM; j++)
{
double g=double(rand()%1000+1)/1000*10*pi;
double nx=qx+delta*cos(g),ny=qy+delta*sin(g);
if(nx<0||ny<0||nx>x||ny>y)continue;
double td=INF;
for(int k = 1; k <= n; k++)
td=min(td,dis(nx,ny,rx[k],ry[k]));
if(td>d[i])
d[i]=td,px[i]=nx,py[i]=ny;
}
}
delta*=0.8;
}
double ans=-233.333;
int pos;
for(int i = 1; i <= NUM; i++)
{
if(d[i]>ans)
{
ans=d[i];
pos=i;
}
}
printf("The safest point is (%.1lf, %.1lf).\n",px[pos],py[pos]);
}
}