POJ2069 最小球体覆盖, 模拟退火

时间:2021-10-24 12:17:32

只是套了个模板,模拟退火具体的过程真心不懂阿

 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = ;
const double eps = 1e-;
int n;
struct POINT{
double x,y,z;
}ps[],q; 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));
} int maxD(POINT p){
double res = ;
int i, k = ;
for(i = ; i < n ; ++i){
double tmp = dist(p,ps[i]);
if(tmp > res){
k = i;
res = dist(p,ps[i]);
}
}
return k;
} double BallCover(){
double step = , ans = INF;
q.x = q.y = q.z = 0.0;
while(step > eps){
int d = maxD(q);
double tmp = dist(ps[d],q);
if(tmp < ans) ans=tmp;
double dx = ps[d].x - q.x;
double dy = ps[d].y - q.y;
double dz = ps[d].z - q.z;
dx /= tmp;
dy /= tmp;
dz /= tmp;
q.x += (dx*step);
q.y += (dy*step);
q.z += (dz*step);
step *= 0.98;
}
return ans;
} int main(){
int i;
while(EOF != scanf("%d",&n)){
if( == n) break;
for(i = ; i < n ; ++i)
scanf("%lf%lf%lf",&ps[i].x,&ps[i].y,&ps[i].z);
printf("%.5f\n",BallCover());
}
return ;
}