题意是求一个到所给点距离之和小的点。
模仿超哥,用退火的方法。( 详细算法解释请看http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html)
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-8
#define maxn 105
#define mod 1000007
#define FOR(i,s,t) for(int i = s; i < t; i++ )
#define LL long long int
#define ULL unsigned long long
#define pii pair<int,int>
//FILE *fin = fopen( "data.in","r" );
struct point {
double x,y,z;
point () {};
point ( double xx,double yy,double zz ): x(xx),y(yy),z(zz) {};
point operator - ( const point &b ) const {
return point( x-b.x,y-b.y, z-b.z);
}
double len (){
return sqrt( x*x+ y*y + z*z );
}
}p[maxn];
int n, dx[30],dy[30],dz[30];
double cal ( point a ){ //计算点a到所有点的距离之和
double ans = 0;
FOR(i,0,n)
ans += (a-p[i]).len();
return ans;
}
int main (){
int cnt = 0;
FOR(i,-1,2)
FOR(j,-1,2)
FOR( k,-1,2)
dx[cnt] = i, dy[cnt] = j, dz[cnt] = k, ++cnt; //当前点的移动方向
while( ~scanf("%d", &n) ){
FOR(i,0,n)
scanf("%lf%lf%lf",&p[i].x, &p[i].y, &p[i].z);
point cur(0,0,0), nxt;
double dis = cal(cur); //dis要满足最优解在其范围之内
double ans = dis;
while( dis > eps ){
FOR(i,0,cnt){ //满足精度退出
nxt = point(cur.x + dx[i]*dis, cur.y + dy[i]*dis, cur.z + dz[i]*dis );
double t = cal(nxt);
if( t < ans ) //找到局部最优解,更新
ans = t, cur = nxt;
}
dis *= 0.99;
}
printf("%.3lf %.3lf %.3lf\n",cur.x,cur.y,cur.z);
}
return 0;
}