[日常摸鱼]poj2420 A Star not a Tree?
题意:给定$n$个点,找一个点使得这个点到所有点的距离之和最小,求出这个最小距离
传说中的模拟退火…
#include<cstdio> #include<ctime> #include<cmath> #include<cstdlib> const int N=105; struct point { double x,y; }ps[N]; int n; double t,ans,tans,tx,ty,nx,ny,temp; inline double get(int x){return 1.0*((rand()%x)*(rand()%x)%x);} inline double sqr(double x){return x*x;} inline double dis(double x,double y,point &p) { return sqrt(sqr(x-p.x)+sqr(y-p.y)); } inline double calc(double x,double y) { double res=0; for(register int i=1;i<=n;i++)res+=dis(x,y,ps[i]); return res; } int main() { scanf("%d",&n);nx=ny=0;t=100000; for(register int i=1;i<=n;i++) { scanf("%lf%lf",&ps[i].x,&ps[i].y); nx+=ps[i].x;ny+=ps[i].y; } nx/=n;ny/=n;tans=ans=calc(nx,ny); while(t>0.02) { tx=ty=0; for(register int i=1;i<=n;i++) { tx+=(ps[i].x-nx)/dis(nx,ny,ps[i]); ty+=(ps[i].y-ny)/dis(nx,ny,ps[i]); } temp=calc(nx+tx*t,ny+ty*t); if(temp<ans) { tans=ans=temp;nx+=tx*t;ny+=ty*t; }else if(log((temp-ans)/t)<get(10000)/10000.0) { ans=temp;nx+=tx*t;ny+=ty*t; } t*=0.9; } if(tans<ans)ans=tans; printf("%.0lf",ans); return 0; }
_(:з」∠)_瞎调参数0ms过掉惹