模拟退火求n个点到某点距离和最短

/*
找出一个点使得这个店到n个点的最长距离最短,即求最小覆盖圆的半径
用一个点往各个方向扩展,如果结果更优,则继续以当前步长扩展,否则缩小步长
*/
#include
#include
#include
const double pi = acos(-1.0);
struct point {
    double x,y;
}p[1010];
int n;
point mid,tmid;
double R,xmin,ymin,xmax,ymax;
double ans,tans;
double dis(point a, point b)
{
    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
void solve(double x,double y)
{
    double max=0;
    point tp;
    tp.x=x;
    tp.y=y;
    for(int i=0;imax)
                max=tmp;
    }
    if(maxxmax) xmax=p[i].x;
            if(p[i].y>ymax) ymax=p[i].y;
        }
        mid.x=(xmin+xmax)/2;
        mid.y=(ymin+ymax)/2;
        tmid=mid;
        R=sqrt((xmax-xmin)*(xmax-xmin)+(ymax-ymin)*(ymax-ymin))/2;
        for(i=0;itans)
                tans=tmp;
        }
        ans=tans;
        while(1)
        {
            for(double j=0;j<=2*pi;j+=0.1)
            {
                solve(mid.x+R*sin(j),mid.y+R*cos(j));
            }
            if(fabs(ans-tans)<0.01&&R<0.0001) break;
            if(tans

你可能感兴趣的:(模拟)