日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/13 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于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;

}

總結

以上是生活随笔為你收集整理的基于matlab 求多边费马点,POJ2420(求多边形费马点) | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。