题意:在三维空间内给出一些行星的坐标,求出把所有点都包含在内的球的最小半径。
这题要用模拟退火,一开始想到了,没想出来怎么把步长跟确定球心坐标联系在一起,后来才发现求出球心到各点最长的距离然后把球心往最远点移动一段距离就可以了。球心自己选的任意点。。。
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const double eps=1e-7; struct point3D { double x,y,z; } data[35]; int n; double dis(point3D a,point3D 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() { double step=100,ans=1e30,mt; point3D z; z.x=z.y=z.z=0; int s=0; while(step>eps) { for(int i=0; i<n; i++) if(dis(z,data[s])<dis(z,data[i])) s=i; mt=dis(z,data[s]); ans=min(ans,mt); z.x+=(data[s].x-z.x)/mt*step; z.y+=(data[s].y-z.y)/mt*step; z.z+=(data[s].z-z.z)/mt*step; step*=0.98; } return ans; } int main() { double ans; while(~scanf("%d",&n),n) { for(int i=0; i<n; i++) scanf("%lf%lf%lf",&data[i].x,&data[i].y,&data[i].z); ans=solve(); printf("%.5f\n",ans); } return 0; }