标签:des style blog http color java os strong
Description
Input
Output
Sample Input
Sample Output
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 10 10 struct point 11 { 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 }
标签:des style blog http color java os strong
原文地址:http://www.cnblogs.com/ERKE/p/3889917.html