[POJ2069]Super Star(最小球覆盖-模拟退火)

题目:

我是超链接

题意:

给n个点的坐标 ( x i , y i , z i ) ,求覆盖这n个点的最小球的半径rr。

题解:

模拟退火,每次往更优秀的方向移动

代码:

#include <cstdio>
#include <cmath>
#include <iostream> 
using namespace std;
const double eps=1e-8;
const double delta=0.98;
const int N=1005;
struct hh{double x,y,z;}a[N];
int n;double c[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
double pf(double x){return x*x;}
double dis(hh a,hh b){return sqrt(pf(a.x-b.x)+pf(a.y-b.y)+pf(a.z-b.z));}
int getpoint(hh now)
{
    int m=1;
    for (int i=2;i<=n;i++)
      if (dis(now,a[m])<dis(now,a[i])) m=i;
    return m;
}
double work()
{
    double ans=1e18;
    double t=100;
    hh now=a[1];
    while (t>eps)
    {
        int m=getpoint(now);
        double r=dis(now,a[m]);
        hh aa;
        aa.x=now.x+(a[m].x-now.x)/r*t;
        aa.y=now.y+(a[m].y-now.y)/r*t;
        aa.z=now.z+(a[m].z-now.z)/r*t;
        ans=min(ans,r);now=aa;
        t*=delta;
    }
    return ans;
}
int main()
{
    while (scanf("%d",&n) && n!=0)
    {
        for (int i=1;i<=n;i++) scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
        printf("%.5lf\n",work());
    }
}

猜你喜欢

转载自blog.csdn.net/blue_cuso4/article/details/80838902
今日推荐