2018南京现场赛D 模拟退火

题目链接:https://codeforces.com/gym/101981/attachments

给你n个城市的三维坐标,叫你求得一个坐标使这个坐标到其他城市的最大距离最小,并输出这个距离(距离不唯一,只要在一定误差内即可)。因为城市的数量不多而且要求的距离不是一个确定值,只需在一定误差内,所以模拟退火跑就完事。

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double eps=1e-8;
#define inf 0x3f3f3f3f
int n;
struct node{
    double x,y,z;
    node(){};
    node(double x,double y,double z):x(x),y(y),z(z){};
    double dis(node w)
    {
        return sqrt((w.x-x)*(w.x-x)+(w.y-y)*(w.y-y)+(w.z-z)*(w.z-z));
    }
}p[105],a;
int main()
{
    scanf("%d",&n);
    double x,y,z;
    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf%lf",&x,&y,&z);
        p[i]=node(x,y,z);
    }
    a=node(0,0,0);
    double T=20000,D=0.98;
    double ans=inf,ret;
    while(T>eps)
    {
        int pos=1;
        for(int i=1;i<=n;i++)
        {
            if(a.dis(p[i])>a.dis(p[pos]))pos=i;
        }
        ret=a.dis(p[pos]);
        ans=min(ans,ret);
        a.x+=(p[pos].x-a.x)/ret*T;
        a.y+=(p[pos].y-a.y)/ret*T;
        a.z+=(p[pos].z-a.z)/ret*T;
        T*=D;
    }
    printf("%.15lf
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/chen99/p/11670317.html