模拟退火。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define maxn 30 #define eps 1e-7 using namespace std; struct point { double x,y,z; }p[maxn],s; int n; double delta=0.98; double dist(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); } double solve() { s.x=s.y=s.z=0; int regis=1; double ans=1e30,t=100; while (t>eps) { for (int i=1;i<=n;i++) if (dist(s,p[i])>dist(s,p[regis])) regis=i; double mt=dist(s,p[regis]); ans=min(ans,mt); s.x+=(p[regis].x-s.x)/mt*t; s.y+=(p[regis].y-s.y)/mt*t; s.z+=(p[regis].z-s.z)/mt*t; t*=delta; } return ans; } int main() { for (;;) { scanf("%d",&n); if (n==0) break; for (int i=1;i<=n;i++) scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z); printf("%.5lf\n",solve()); } return 0; }
时间: 2024-10-10 18:14:38