您的位置:首页 > 其它

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

2014-08-27 13:06 204 查看
只是套了个模板,模拟退火具体的过程真心不懂阿

//#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 = 220;
const double eps = 1e-8;
int n;
struct POINT{
double x,y,z;
}ps[35],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 = 0;
int i, k = 0;
for(i = 0; 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 = 100, 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(0 == n)  break;
for(i = 0; i < n ; ++i)
scanf("%lf%lf%lf",&ps[i].x,&ps[i].y,&ps[i].z);
printf("%.5f\n",BallCover());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: