POJ 2420 A Star not a Tree?(二维费马点)

转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove

题目:就是求多边形的费马点,输出最小的距离。

http://poj.org/problem?id=2420

做法:随机化变步长贪心法(模拟退火???)

首先随机选出一点,我直接取了0,0

然后选定一个步长,往4个方向开始找,如果更优则继续,否则降低步长,直到满足题目要求精度

也可以8个方向等等

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<sstream>
#include<cassert>
#define LL long long
#define eps 1e-6
#define inf 1ll<<50
#define zero(a) fabs(a)<eps
#define N 20005
using namespace std;
struct Point{
    double x,y;
}p[105],cur,pre;
int n;
int way[4][2]={0,1,0,-1,1,0,-1,0};
double dist(Point p1,Point p2){
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double get_dist(Point cen){
    double sum=0;
    for(int i=0;i<n;i++)
        sum+=dist(cen,p[i]);
    return sum;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        pre.x=0;pre.y=0;
        double step=100,best=get_dist(pre);
        while(step>0.2){
            bool ok=true;
            while(ok){
                ok=false;
                for(int i=0;i<4;i++){
                    cur.x=way[i][0]*step+pre.x;
                    cur.y=way[i][1]*step+pre.y;
                    double dis=get_dist(cur);
                    if(dis<best){best=dis;pre=cur;ok=true;}
                }
            }
            step/=2.0;
        }
        printf("%d\n",(int)(best+0.5)*100/100);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值