Super Star OpenJ_Bailian - 2069(还有问题)空间最小覆盖球

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/82263707
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;


struct Point
{
    double x,y,z;
    Point(){}
    Point(double _x,double _y,double _z)
    {
        x=_x;
        y=_y;
        z=_z;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y,z-b.z);
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y+z*b.z;
    }
};

const int maxn=50;
const double T=100;//起始温度
const double delta=0.98;//梯度
const double eps=1e-9;

Point p[maxn];


double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
double SA(int n)
{
    double t=T;
    Point s=p[0];
    double ans=1e99;
    while(t>eps)
    {
        int k=0;
        double len=-1;
        for(int i=0;i<n;i++)
        {
            if(dist(s,p[i])>dist(s,p[k]))
                k=i;
        }
        double lens=dist(s,p[k]);
        ans=min(ans,lens);
        s.x=s.x+(p[k].x-s.x)/lens*t;
        s.y=s.y+(p[k].y-s.y)/lens*t;
        s.z=s.z+(p[k].z-s.z)/lens*t;
        t=t*delta;
    }
    return ans;
}


int main()
{
    int n;
    while(1)
    {
        scanf("%d",&n);
        if(n==0)
            break;
        for(int i=0;i<n;i++)
            scanf("%lf %lf %lf",&p[i].x,&p[i].y,&p[i].z);
        printf("%.5lf\n",SA(n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/82263707