您的位置:首页 > 其它

POJ 2420 A Star not a Tree?(模拟退火)

2013-10-21 10:26 447 查看
题目链接

居然1Y了,以前写的模拟退火很靠谱啊。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
struct point
{
double x,y;
}p[201];
int n;
int a[4] = {0,0,-1,1};
int b[4] = {1,-1,0,0};
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 = 0;
int i;
for(i = 0;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 = 0;i < n;i ++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
key = 5;
ans = 100000000;
num = 200;
x = p[0].x;
y = p[0].y;
T = 40;
while(T--)
{
for(i = 1;i <= num;i ++)
{
for(j = 0;j < 4;j ++)
{
k = rand()%key;
tx = x + k*a[j]*T;
ty = y + k*b[j]*T;
if(tx >= 0&&tx <= 10000&&ty >= 0&&ty <= 10000)
{
d = fun(tx,ty);
if(ans > d)
{
ans = d;
x = tx;
y = ty;
}
}
}
}
}
printf("%.f\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: