题意:在给定的范围内找一点,使得该点到所有点距离最小距离最大,求这个最大距离。
分析:模拟退火算法,先随便找一个点,然后运用模拟退火算法。
退火算法参考:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html
#include <iostream>
#include<cstdio>
#include<ctime>
#include<cmath>
#include<cstdlib>
using namespace std;
#define N 1005
#define INF 999999999
#define D 25
#define EPS 1e-3
double a[N],b[N];
int mo[8][2]={1,0,0,1,-1,0,0,-1,1,-1,-1,1,-1,-1,1,1};
int main()
{
int t;
scanf("%d",&t);
srand((unsigned int)time(NULL));
while(t--)
{
int x,y,m;
scanf("%d%d%d",&x,&y,&m);
for(int i=0;i<m;i++)
{
scanf("%lf%lf",&a[i],&b[i]);
}
double T=sqrt((double)x*x+y*y)+10;
double ansx=(double)(rand()%1000+1)/1000.000*x;
double ansy=(double)(rand()%1000+1)/1000.000*y;
double ansdis=INF;
for(int i=0;i<m;i++)
{
ansdis=min(ansdis,sqrt((ansx-a[i])*(ansx-a[i])+(ansy-b[i])*(ansy-b[i])));
}
while(T>EPS)
{
for(int k=0;k<D;k++){
for(int j=0;j<8;j++){
double tempx=ansx+(double)(rand()%1000+1)/1000.000*T*mo[j][0];
double tempy=ansy+(double)(rand()%1000+1)/1000.000*T*mo[j][1];
if(tempx<0||tempx>x||tempy<0||tempy>y)
continue;
double tempdis=INF;
for(int i=0;i<m;i++)
{
tempdis=min(tempdis,sqrt((tempx-a[i])*(tempx-a[i])+(tempy-b[i])*(tempy-b[i])));
}
if(tempdis>ansdis)
{
ansx=tempx;
ansy=tempy;
ansdis=tempdis;
}
}
}
T=T*0.9;
}
printf("The safest point is (%.1f, %.1f).\n",ansx,ansy);
}
return 0;
}