ZOJ 2928 模拟退火 求三维费马点
2014-03-28 21:21
344 查看
#include<iostream> #include<cstring> #include<string> #include<cstdio> #include<algorithm> #include<vector> #include<map> #include<queue> #include<cmath> using namespace std; #define eps 1e-12 #define N 110 #define M 2001000 #define ll long long #define inf 0x3f3f3f3f int n,cnt; struct P { double x,y,z; P(){} P(double _x,double _y,double _z) :x(_x),y(_y),z(_z){} double len() { return sqrt(x*x+y*y+z*z); } P operator+(const P p) { return P(x+p.x,y+p.y,z+p.z); } P operator-(const P p) { return P(x-p.x,y-p.y,z-p.z); } P operator*(double c) { return P(x*c,y*c,z*c); } }p ,dir[30]; int dblcmp(double x) { return (x>eps)-(x<-eps); } void init() { for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++) for(int k=-1;k<=1;k++) dir[cnt++] = P(i,j,k); } void solve() { double step = 100; double mi = 1e10; P ans = P(0,0,0),now,nxt; for(int j=0;j<n;j++) ans = ans+p[j]; ans = ans * (1.0/n); while(step>eps) { nxt = ans; for(int i=0;i<cnt;i++) { now = ans+dir[i]*step; double tmp = 0; for(int j=0;j<n;j++) tmp += (now-p[j]).len(); if(dblcmp(tmp-mi)<0) { mi = tmp; nxt = now; } } ans = nxt; step *= 0.993; } printf("%.3lf %.3lf %.3lf\n",ans.x,ans.y,ans.z); } int main() { init(); while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z); solve(); } return 0; }
相关文章推荐
- 【模拟退火】POJ 2420/ZOJ 1901 费马点
- poj 2420 A Star not a Tree?(模拟退火求费马点)
- 【BZOJ3680】吊打XXX 广义费马点 模拟退火
- poj 2420 A Star not a Tree?(模拟退火求费马点)
- zoj 1940 模拟三维bfs
- POJ 2420 模拟退火求费马点
- POJ 2420 A Star not a Tree?(计算几何 多边形的费马点 模拟退火)
- 【模拟退火,广义费马点】POJ2420 A Star not a Tree?
- POJ 2420 A Star not a Tree? 费马点 计算几何 模拟退火
- BZOJ 3680 吊打XXX 计算几何 模拟退火 广义费马点
- POJ2420 费马点问题求解 随机化 模拟退火
- 模拟退火求二维费马点
- [hdu3644 A Chocolate Manufacturer's Problem]模拟退火,简单多边形内最大圆
- bzoj2428 [ HAOI2006 ] -- 模拟退火
- zoj 2417 Lowest Bit(简单的模拟)
- ZOJ 3829 模拟贪心
- ZOJ 3326 An Awful Problem【时间区间天数(模拟)】
- poj 1379 Run Away 计算几何 模拟退火
- Matlab数模笔记(7)--模拟退火模型
- ZOJ 3826 大模拟