[POJ2420]A Star not a Tree?(模拟退火)

题目:

我是超链接

题意:

平面上给你n个点,让你求一个点,到这n点的距离和最小。

题解:

这里写图片描述
好玄学啊。。伪代码献上

T=100.0;               //初始温度

for(int i=0;i<100;i++)      //控制迭代次数
{
    tar=getPos();           //在x的周围选一个点
    E=f(tar)-f(x);

    if(E > eps) x=tar;      //直接移动
    else if(exp(E/T) > random(0,1)) x=tar;     //接受移动

    T=T*0.99;               //降温
}

模拟退火的讲解引自这个up

这个题目明显有一个更优的趋势,我们用模拟退火

代码:

#include <cstdio>
#include <cmath>
#include <iostream> 
using namespace std;
const double eps=1e-8;
const double delta=0.98;
const int N=1005;
struct hh{double x,y;hh(double X=0,double Y=0){x=X;y=Y;};}a[N];
int n;double c[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
double pf(double x){return x*x;}
double getdis(double x,double y)
{
    double sum=0;
    for (int i=1;i<=n;i++)
      sum+=sqrt(pf(x-a[i].x)+pf(y-a[i].y));
    return sum;
}
double work()
{
    double ans=1e18;
    double t=100;
    hh now=a[1];
    while (t>eps)
    {
        bool fin=1;
        while (fin)
        {
            fin=0;
            for (int i=0;i<4;i++)
            {
                double x=now.x+c[i][0]*t,y=now.y+c[i][1]*t;
                double hx=getdis(x,y);
                if (hx<ans) {fin=1;now=hh(x,y);ans=hx;}
            }
        }
        t*=delta;
    }
    return ans;
}
int main()
{
    while (scanf("%d",&n)!=EOF)
    {
        for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
        printf("%.0lf\n",work());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值