POJ 2069 super star(三维点集最小圆覆盖 模拟退火)

模拟退火思想,开始就用普通的随机模拟退火去做,怎么都是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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值