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

 题目大意:在多边形中找一个点,让这个点距离所有点的距离和最短。
          模拟退火,选一个随机点,朝上,下,左,右四个点移动
          代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define maxn 100+10
#define sqr(x) ((x)*(x))
#define F(i,n) for(int i=0;i<n;i++)
#define eps 1e-3
#define pi acos(-1.0)
#define Min(a,b) a>b?b:a
using namespace std;
int dr[4][2]={-1,0,0,-1,1,0,0,1};
struct cpoint{    
double x,y,d;    
cpoint()    
{
  d=0;    
}
};
cpoint cp[maxn],rp;
int n;
double dis(cpoint p,cpoint q)
{    
  return sqrt(sqr(p.x-q.x)+sqr(p.y-q.y));
}
void solve()
{    
    rp=cp[0];
    rp.d=0;
    F(i,n)
     rp.d+=dis(rp,cp[i]);     //printf("rp :%lf\n",rp.d);
    double dal=10000;
    while(dal>0.2)
    {
            cpoint t;
            F(j,4)
            {
                t.x=rp.x+dal*dr[j][0];
                t.y=rp.y+dal*dr[j][1];
                if(t.x>10000||t.x<0||t.y>10000||t.y<0)
                   continue;
                t.d=0;
                F(k,n)
                {
                    t.d+=dis(t,cp[k]);
                }                //printf("d :%lf\n",t.d);
                if(rp.d>t.d)
                   rp=t;
            }        
dal*=0.5;
    }
    printf("%d\n",(int)(rp.d+0.5)*100/100);
}
int main()
{    
while(scanf("%d",&n)!=EOF)
    {       
      F(i,n)
      scanf("%lf %lf",&cp[i].x,&cp[i].y);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值