UVA10228 A Star not a Tree?
出个好好的题卡什么输出格式
很简单的模拟退火
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<ctime>
using namespace std;
double delat=0.995;
struct po{
double x;double y;
}p[10005];
double ansx,ansy,ans;
double sx,sy;
int t;
int n;
double cacultion(double x,double y){
double ans=0;
for(int i=1;i<=n;++i){
double xx=p[i].x-x;
double yy=p[i].y-y;
ans+=sqrt(xx*xx+yy*yy);
}
return ans;
}
void sa(){
double nx=ansx;
double ny=ansy;
double t=5000;
while(t>1e-14){
double nex=nx+((rand()<<1)-RAND_MAX)*t;
double ney=ny+((rand()<<1)-RAND_MAX)*t;
double nd=cacultion(nex,ney);
double ex=nd-ans;
if(ex<0){
ansx=nex;
ansy=ney;
nx=nex;
ny=ney;
ans=nd;
} else{
if(exp(-ex/t)*RAND_MAX>rand()){
nx=nex;
ny=ney;
}
}
t*=delat;
}
}
signed main(){
srand(time(NULL));
rand();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
sx=0.0;
sy=0.0;
for(int i=1;i<=n;++i){
cin>>p[i].x>>p[i].y;
sx+=p[i].x;
sy+=p[i].y;
}
ansx=(double)sx/n;
ansy=(double)sy/n;
ans=1e8;
sa();
sa();
sa();
sa();
sa();
sa();
sa();
sa();
sa();
sa();
if(t==0){
cout<<round(ans)<<endl;
}else{
cout<<round(ans)<<"\n\n";
}
}
return 0;
}