费马点问题

求费马点,用左右上下来确定距离各点最短的点

#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <bitset>
#include <stack>
#include <ctime>
#include <list>
#define INF 0x7fffffff
#define max3(a,b,c) (max(a,b)>c?max(a,b):c)
#define min3(a,b,c) (min(a,b)<c?min(a,b):c)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define maxn 200

struct point
{
    double x, y;
} p[maxn];

int n;

point update(double x, double y)
{
    point tmp;
    tmp.x = x;
    tmp.y = y;
    return tmp;
}

double dis(point a, point b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
double  sumdis(point a)
{
    double ans = 0;
    for(int i = 0; i < n; ++i)
        ans += dis(a, p[i]);
    return ans;
}
int main()
{
    while(scanf("%d", &n) == 1)
    {
        for(int i = 0; i < n; ++i)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        point pp = p[0];
        double ans = INF;
        double step = 100;
        ans = sumdis(pp);
        while(step>0.2)
        {
            bool flag=true;
            while(flag)
            {
                flag = false;
                point Q = update(pp.x, pp.y + step);
                point tt = pp;
                double temp = sumdis(Q);
                if(temp < ans)
                {
                    ans = temp;
                    tt = Q;
                    flag = true;
                }
                Q = update(pp.x, pp.y - step);
                temp = sumdis(Q);
                if(temp < ans)
                {
                    ans = temp;
                    tt = Q;
                    flag = true;
                }
                Q = update(pp.x + step, pp.y);
                temp = sumdis(Q);
                if(temp < ans)
                {
                    ans = temp;
                    tt = Q;
                    flag = true;
                }

                Q = update(pp.x - step, pp.y);
                temp = sumdis(Q);
                if(temp < ans)
                {
                    ans = temp;
                    tt = Q;
                    flag = true;
                }

                pp = tt;
            }
            step  /= 2.0;
        }
        int dis = (int)(ans + 0.5)*100/100;

        printf("%d\n", dis);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值