您的位置:首页 > 其它

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

2014-04-30 23:41 441 查看
。。。我想说的是听着模拟退火这么高端的一个算法。。。这么感觉这么简单呢。。。嗯,一定是我才开始写的原因。。。再多做几题感受一下

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
struct P
{
double x,y;
}p[105];
int n;
double dis(P a,P b)
{
double c=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
return sqrt(c);
}
double alldis(P r)
{
double ans=0;
for(int i=0;i<n;i++)
ans+=dis(r,p[i]);
return ans;
}
bool eque(P a,P b)
{
if(a.x==b.x && a.y==b.y)
return 1;
return 0;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
double step=100;
double mine;
P r;
r=p[0];
mine=alldis(r);
double k;
while(step>0.02)
{
P a,c=r;

a=r;
a.x+=step;
k=alldis(a);
if(k<mine)
{
mine=k;
c=a;
}

a=r;
a.x-=step;
k=alldis(a);
if(k<mine)
{
mine=k;
c=a;
}

a=r;
a.y+=step;
k=alldis(a);
if(k<mine)
{
mine=k;
c=a;
}

a=r;
a.y-=step;
k=alldis(a);
if(k<mine)
{
mine=k;
c=a;
}
if(eque(c,r))
step/=2;
else
r=c;
}
printf("%.0lf\n",k);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: