您的位置:首页 > 其它

【POJ 2420】A Star not a Tree?

2017-03-19 19:04 316 查看
其实就是找一个费马点。

我感觉这玩意儿根本不叫模拟退火啊,这个退火的概率都没出现,唯一有点退火的就是搜索的范围逐渐减小。

每次朝着上下左右的更优方向走,暴力的步长为1,模拟退火的步长就是当前温度,也就是说搜索的范围会越来越小,也越接近费马点。

坑爹啊竟然精度这么点,一开始double强制转int死活过不了,后来才知道要四舍五入。。。

#include<cmath>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iomanip>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
#define mod 1000000007
#define N 200
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
int n,i,j,T;
int cnt;
double x
,y
;
double ans,px,py,nx,ny;
const int max_rand = 2333333;
void calc(double qx,double qy,double &s)
{
int i; s = 0;
fo(i,1,n) s += hypot(x[i]-qx,y[i]-qy);
}
double solve()
{
int i; double res,dis;
const double max_tmp = 10000;
const double dec = 0.5;
double tmp = max_tmp;
px = x[1]; py = y[1]; calc(px,py,res);
while (tmp > 0.02)
{
fo(i,-1,1)
fo(j,-1,1)
{
nx = px + tmp * i;
ny = py + tmp * j;
calc(nx,ny,dis);
//if (accept(dis2-dis1,tmp))
if (dis < res)
px = nx,py = ny,res = dis;
}
tmp *= dec;
}
return res;
}

int main()
{
srand(233333);
scanf("%d",&n);
fo(i,1,n) scanf("%lf%lf",&x[i],&y[i]);
printf("%d\n",(int)(solve()+0.5));
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟退火