模拟退火初探--POJ 2069 Super Star

来源:互联网 发布:大数据上市公司有几家 编辑:程序博客网 时间:2024/05/17 11:59

Super Star

Description

During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found strange synchronized movements of stars. Having heard these observations, Dr. Extreme proposed a theory of “super stars”. Do not take this term as a description of actors or singers. It is a revolutionary theory in astronomy.
According to this theory, starts we are observing are not independent objects, but only small portions of larger objects called super stars. A super star is filled with invisible (or transparent) material, and only a number of points inside or on its surface shine. These points are observed as stars by us.

In order to verify this theory, Dr. Extreme wants to build motion equations of super stars and to compare the solutions of these equations with observed movements of stars. As the first step, he assumes that a super star is sphere-shaped, and has the smallest possible radius such that the sphere contains all given stars in or on it. This assumption makes it possible to estimate the volume of a super star, and thus its mass (the density of the invisible material is known).

You are asked to help Dr. Extreme by writing a program which, given the locations of a number of stars, finds the smallest sphere containing all of them in or on it. In this computation, you should ignore the sizes of stars. In other words, a star should be regarded as a point. You may assume the universe is a Euclidean space.

Input

The input consists of multiple data sets. Each data set is given in the following format.

n
x1 y1 z1
x2 y2 z2

xn yn zn

The first line of a data set contains an integer n, which is the number of points. It satisfies the condition 4 <= n <= 30.

The location of n points are given by three-dimensional orthogonal coordinates: (xi, yi, zi) (i = 1, …, n). Three coordinates of a point appear in a line, separated by a space character. Each value is given by a decimal fraction, and is between 0.0 and 100.0 (both ends inclusive). Points are at least 0.01 distant from each other.

The end of the input is indicated by a line containing a zero.

Output

For each data set, the radius of the smallest sphere containing all given points should be printed, each in a separate line. The printed values should have 5 digits after the decimal point. They may not have an error greater than 0.00001.

Sample Input

4
10.00000 10.00000 10.00000
20.00000 10.00000 10.00000
20.00000 20.00000 10.00000
10.00000 20.00000 10.00000
4
10.00000 10.00000 10.00000
10.00000 50.00000 50.00000
50.00000 10.00000 50.00000
50.00000 50.00000 10.00000
0

Sample Input

7.07107
34.64102

题意:

采用类似退火算法逼近求解:一开始另一点等于所有点的平均值(几何中心),然后在第一次遍历之后,先取几何中心和第一个点的值为假想最大半径,然后遍历出实际最大半径,接下来将半径放在几何中心与最大半径的另一个端点处的k等比分点,这样N次迭代,每次我们取的点都是在于目前最大值和前一次假想最优解的k等比分点,同时k每次是之前的q倍,q小于接近于1,即可求出理想的解。

#include<iostream>#include<cstring>#include<cmath>#include<iomanip>using namespace std;struct node{    double x,y,z;};ostream& operator << (ostream& ot,node x){    ot<<'('<<x.x<<','<<x.y<<','<<x.z<<')';    return ot;}double sqr(double x,double y){    return (x-y)*(x-y);}double dis(node a,node b){    return sqrt(sqr(a.x,b.x)+sqr(a.y,b.y)+sqr(a.z,b.z));}int n;const int maxn=110;node rec[maxn];inline double ac(int tms,node ans,double rxo,double step) //tms迭代次数,ans初始点(几何中心),rxo等比数列初值,step等比数列公比{    double s=dis(ans,rec[0]);    while(tms--)    {        int maxi=0;        s=dis(ans,rec[0]);        for(int i=1;i<n;i++)        {            double tem=dis(ans,rec[i]);            if(tem>s)            {                s=tem;                maxi=i;            }        }        ans.x+=rxo*(rec[maxi].x-ans.x);        ans.y+=rxo*(rec[maxi].y-ans.y);        ans.z+=rxo*(rec[maxi].z-ans.z);        rxo*=step;    }    return s;}int main(){    ios::sync_with_stdio(0);    while(cin>>n&&n)    {        node pp;        pp.x=0;        pp.y=0;        pp.z=0;        for(int i=0;i<n;i++)        {            cin>>rec[i].x>>rec[i].y>>rec[i].z;            pp.x+=rec[i].y;            pp.y+=rec[i].y;            pp.z+=rec[i].z;        }        pp.x/=n;        pp.y/=n;        pp.z/=n;        double res=ac(30000,pp,0.5,0.9803125);//若初值取中点,即0.5,实测q<=0.98会wa,0.9803125刚好可AC        cout<<fixed<<setprecision(5)<<res<<'\n';    }    return  0;}
原创粉丝点击