求费马点,用左右上下来确定距离各点最短的点
#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;
}