求多边形的费马点,,爬山算法
#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;
}