POJ 2420 A Star not a Tree?

27 篇文章 0 订阅
1 篇文章 0 订阅

求多边形的费马点,,爬山算法

#include "cstring"
#include "iostream"
#include "algorithm"
#include "cstdio"
#include "queue"
#include "set"
#include "cmath"
using namespace std;
typedef long long LL;
const int M=510;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.00);
const double EPS = 1e-6;


struct node
{
    double x,y;

} p[105],a,b;

int n;
int dir[4][2]= {1,0,-1,0,0,-1,0,1};

double dist(node n,node m)
{
    return sqrt((n.x-m.x)*(n.x-m.x)+(n.y-m.y)*(n.y-m.y));
}

double maxn(node q)
{
    double s=0;
    for(int i=0; i<n; ++i)
    {
        s+=dist(q,p[i]);

    }
    return s;
}


int main()
{
    cin>>n;
    node t;
    t.x=0,t.y=0;
    for(int i=0; i<n; ++i)
    {
        cin>>p[i].x>>p[i].y;
        // t.x+=p[i].x;
        // t.y+=p[i].y;
    }
    // t.x/=n;
    // t.y/=n;
    t.x=t.y=0;
    double ans;
    double step=100;
    ans=maxn(t);
    while(step>0.2)
    {
        int flag=1;
        while(flag)
        {
            flag=0;
            for(int i=0; i<4; ++i)
            {
                b.x=step*dir[i][0]+t.x;
                b.y=step*dir[i][1]+t.y;
                double temp=maxn(b);
                if(temp<ans)
                {
                    ans=temp;
                    t=b;
                    flag=1;
                }
               // cout<<temp<<endl;
            }

        }
        step/=2.0;
    }

    cout<<int(ans+0.5)<<endl;

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值