南京赛的最小球覆盖,求最小的球的半径 南京赛最小球覆盖
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
const int maxn = 100+20;
const double eps = 1e-3;
using namespace std;
int n;
struct node{
double x;
double y;
double z;
}e[maxn];
double f3(double x,double y,double z){
double ans = 0;
for(int i = 0 ; i < n ; i++)
ans = max(ans,(e[i].x-x)*(e[i].x-x) + (e[i].y-y)*(e[i].y-y) + (e[i].z-z)*(e[i].z-z));
return ans;
}
double f2(double x,double y){
double l = -100000.0;
double r = 100000.0;
while(r - l > eps){
double lmid = l + (r-l)/3.0;
double rmid = r - (r-l)/3.0;
if(f3(x,y,lmid) < f3(x,y,rmid)) r = rmid;
else l = lmid;
}
return f3(x,y,l);
}
double f1(double x){
double l = -100000.0;
double r = 100000.0;
while(r - l > eps){
double lmid = l + (r-l)/3.0;
double rmid = r - (r-l)/3.0;
if(f2(x,lmid) < f2(x,rmid)) r = rmid;
else l = lmid;
}
return f2(x,l);
}
int main(){
while(~scanf("%d",&n)){
for(int i = 0 ; i < n ; i++){
scanf("%lf%lf%lf",&e[i].x,&e[i].y,&e[i].z);
}
double l = -100000.0, r = 100000.0;
while(r - l > eps){
double lmid = l + (r-l)/3.0;
double rmid = r - (r-l)/3.0;
if(f1(lmid) < f1(rmid)) r = rmid;
else l = lmid;
}
printf("%.5f\n",sqrt(f1(l)));
}
return 0;
}