居然1Y了,以前写的模拟退火很靠谱啊。
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
struct point
{
double x,y;
}p[];
int n;
int a[] = {,,-,};
int b[] = {,-,,};
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y2-y1)*(y2-y1));
}
double fun(double x,double y)
{
double ans = ;
int i;
for(i = ;i < n;i ++)
{
ans += dis(p[i].x,p[i].y,x,y);
}
return ans;
}
int main()
{
int T,i,key,num,j,k;
double tx,ty,ans,d,x,y;
srand(time(NULL));
scanf("%d",&n);
for(i = ;i < n;i ++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
key = ;
ans = ;
num = ;
x = p[].x;
y = p[].y;
T = ;
while(T--)
{
for(i = ;i <= num;i ++)
{
for(j = ;j < ;j ++)
{
k = rand()%key;
tx = x + k*a[j]*T;
ty = y + k*b[j]*T;
if(tx >= &&tx <= &&ty >= &&ty <= )
{
d = fun(tx,ty);
if(ans > d)
{
ans = d;
x = tx;
y = ty;
}
}
}
}
}
printf("%.f\n",ans);
return ;
}