基于matlab 求多边费马点,POJ2420(求多边形费马点) | 学步园

题意:题目的意思就是给你N个点,在平面上寻找一个点,使得这个点到其他点的距离之和最小,问你最小的距离是多

少?

分析:在三角形内部这个点叫做费马点(费马点定义)。那么这道题目就是求一个多变形的费马点。这样的话,我们可

以像搜索那样,先定义一个起点,然后以这个点为基点分别向上、向下、向左、向右移动,只要使得距离之和变小的移

动都是有效的移动。那么我们可以从第一点开始,(这种方法有点像上次亮神A的那道题目就是从三角形内部一点开始

上下左右移动求最小的值)。那么结束的条件就是题目的卡的精度。这样的就是像搜索一样找就可以了。

代码:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define INF 0x7fffffff

#define max3(a,b,c) (max(a,b)>c?max(a,b):c)

#define min3(a,b,c) (min(a,b)

#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;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值