Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3932
Note that the distance from the farthest point is 0. Therefore, set the initial value to-1.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ctime>#include<cmath>#include<cstdlib>#define db doubleusing namespace std;const int N=1005;const db dc=0.99,eps=1e-17,INF=2e4;int n,nx[N],ny[N],pid;db ans,px,py;int rdn(){ int ret=0;bool fx=1;char ch=getchar(); while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)fx=0;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘) ret=(ret<<3)+(ret<<1)+ch-‘0‘,ch=getchar(); return fx?ret:-ret;}db dis(db x0,db y0,db x1,db y1){ return sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));}void SA(db T){ db x0=px,y0=py,pr=ans;int id=pid; while(T>eps) { pr=-1; for(int i=1;i<=n;i++) { db tmp=dis(x0,y0,nx[i],ny[i]); if(tmp>pr)pr=tmp,id=i; } if(pr!=-1&&pr<ans)ans=pr,pid=id,px=x0,py=y0; x0=x0+(nx[id]-x0)/pr*T; y0=y0+(ny[id]-y0)/pr*T; T*=dc; }}int main(){ srand(time(0)); int tx,ty; while(scanf("%d%d%d",&tx,&ty,&n)==3) { for(int i=1;i<=n;i++)nx[i]=rdn(),ny[i]=rdn(),px+=nx[i],py+=ny[i]; px/=n; py/=n; ans=INF; SA(10000);SA(10000); printf("(%.1lf,%.1lf).\n%.1lf\n",px,py,ans); } return 0;}
HDU 3932 groundhog build home -- Simulated Annealing