模拟退火思想,开始就用普通的随机模拟退火去做,怎么都是wa,后来看网上的解答,每次都是像最远点逼近
也就是每次朝最优路径走!
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 100
#define eps 1e-10
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
struct point{
double x,y,z;
}po[maxn],cir;
int n;
double step;
double dis(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 find_max(point &a,int &num){
double ans=0,t;
num=0;
for(int i=0;i<n;i++){
t=dis(po[i],a);
if(t>ans) ans=t,num=i;
}
return ans;
}
int solve(){
double ans=1e12,d,a,b,c,t;
int i,j,k,pos;
cir=po[0];
step=find_max(cir,pos);
while(step>eps){
d=find_max(cir,pos);
ans=MIN(ans,d);
a=(po[pos].x-cir.x)/d;
b=(po[pos].y-cir.y)/d;
c=(po[pos].z-cir.z)/d;
cir.x+=step*a;
cir.y+=step*b;
cir.z+=step*c;
step*=0.99;
}
printf("%.5lf\n",ans);
return 0;
}
int main(){
int i,j,k;
while(scanf("%d",&n),n){
for(i=0;i<n;i++)
scanf("%lf%lf%lf",&po[i].x,&po[i].y,&po[i].z);
if(n==1){
printf("%.5f\n",0.0);
continue;
}
if(n==2){
printf("%.5lf\n",dis(po[0],po[1])/2);
continue;
}
solve();
}
return 0;
}