首页 > 代码库 > 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 completely harmless at the first sight. But when activated, they start to throw out very hot java, uh ... pardon, lava. Unfortunately, all known paths to the Center Room (where the Sarcophagus is) contain a trigger that activates the trap. The ACM were not able to avoid that. But they have carefully monitored the positions of all the holes. So it is important to find the place in the Large Room that has the maximal distance from all the holes. This place is the safest in the entire room and the archaeologist has to hide there.
 

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing three integers X, Y, M separated by space. The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= M <= 1000. The numbers X and Yindicate the dimensions of the Large Room which has a rectangular shape. The number M stands for the number of holes. Then exactly M lines follow, each containing two integer numbers Ui and Vi (0 <= Ui <= X, 0 <= Vi <= Y) indicating the coordinates of one hole. There may be several holes at the same position.
 

Output

Print exactly one line for each test case. The line should contain the sentence "The safest point is (P, Q)." where P and Qare the coordinates of the point in the room that has the maximum distance from the nearest hole, rounded to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1). 
 

Sample Input

3 1000 50 1 10 10 100 100 4 10 10 10 90 90 10 90 90 3000 3000 4 1200 85 63 2500 2700 2650 2990 100
 

Sample Output

The safest point is (1000.0, 50.0). The safest point is (50.0, 50.0). The safest point is (1433.0, 1669.8).
 
 1 #include <iostream> 2 #include <math.h> 3 #include <stdio.h> 4 #include <string.h> 5 #include <time.h> 6 #include <algorithm> 7 using namespace std; 8 #define N 50 9 #define M 1010 struct point11 {12     double x,y,mina;13 };14 point p[1100],s,tri[100];15 int n;16 double dist(point a,point b)17 {18     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);19 }20 double getmina(point tem)21 {22     double mina=1e9,t;23     for(int i=0; i<n; i++)24     {25         t=dist(tem,p[i]);26         if(t<mina)27             mina=t;28     }29     return mina;30 }31 void Simulated_Annealing()32 {33     int i,j;34     for(i=0; i<N; i++)35     {36         tri[i].x=((rand()%1000)+1.0)/1000.0*s.x;37         tri[i].y=((rand()%1000)+1.0)/1000.0*s.y;38         tri[i].mina=getmina(tri[i]);39     }40     double temper=s.x+s.y,dx,dy;41     point tmp;42     while(temper>0.001)43     {44         for(i=0; i<N; i++)45         {46             for(j=0; j<M; j++)47             {48                 dx=((rand()%1000)+1.0)/1000.0*temper;49                 dy=sqrt(temper*temper-dx*dx);50                 if (rand()&1) dx*=-1;51                 if (rand()&1) dy*=-1;52                 tmp.x=tri[i].x+dx;53                 tmp.y=tri[i].y+dy;54                 if (tmp.x>=0 && tmp.x<=s.x && tmp.y>=0 && tmp.y<=s.y)55                 {56                      tmp.mina=getmina(tmp);57                     if(tmp.mina>tri[i].mina)58                     {59                         tri[i]=tmp;60                     }61                 }62             }63         }64         temper*=0.6;65     }66     int mini=0;67     for(i=1;i<N;i++)68     if(tri[mini].mina<tri[i].mina)69     mini=i;70       printf("The safest point is (%.1lf, %.1lf).\n",tri[mini].x,tri[mini].y);71 }72 int main()73 {74     srand((unsigned int)(time(NULL)));75     int t,i;76     scanf("%d",&t);77     while(t--)78     {79         scanf("%lf%lf%d",&s.x,&s.y,&n);80         for(i=0; i<n; i++)81         {82             scanf("%lf%lf",&p[i].x,&p[i].y);83         }84         Simulated_Annealing();85     }86 }
View Code