最小费马点
以POJ的2420为例来说明一下,我感觉这个应该算得上是二分吧.先进行点的变换,确定当前最优,在二分距离,这样应该就能得出答案了吧!
#include <iostream>
#include<cstdio>
#include<string.h>
#include<cmath>
using namespace std;
struct point
{
double x,y;
point (double a=0,double b=0){x=a;y=b;}
}p[105];
double work1(point a,int n)
{
int i;
double ans;
ans=0;
for(i=0;i<n;i++)
ans+=sqrt((a.x-p[i].x)*(a.x-p[i].x)+(a.y-p[i].y)*(a.y-p[i].y));
return ans;
}
int main()
{
int n,i;
double std,ans,temp;
bool f;
point pp,tt,R;
while(scanf("%d",&n)==1)
{
std=100;
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
pp=p[0];
ans=work1(pp,n);
while(std>0.1)
{
f=1;
while(f)
{
f=0;
R=point(pp.x+std,pp.y);
temp=work1(R,n);
if(ans>temp)
{
f=true;
tt=R;
ans=temp;
}
R=point(pp.x-std,pp.y);
temp=work1(R,n);
if(ans>temp)
{
f=true;
tt=R;
ans=temp;
}
R=point(pp.x,pp.y+std);
temp=work1(R,n);
if(ans>temp)
{
f=true;
tt=R;
ans=temp;
}
R=point(pp.x,pp.y-std);
temp=work1(R,n);
if(ans>temp)
{
f=true;
tt=R;
ans=temp;
}
if(f)
pp=tt;
}
std=std/2;
}
printf("%0.lf\n",ans);
}
return 0;
}

浙公网安备 33010602011771号