Gym - 101981D Country Meow(模拟退火)

题意

三维空间有\(n\)个点,找到另外一个点,离所有点的最大距离最小。求这个距离。

题解

\(1\)、最小球覆盖,要找的点为球心。

\(2\)、模拟退火。

还是补一下模拟退火的介绍吧。

模拟退火有一个初始温度,温度越高,接受较差的解的可能性就越大。每次走完后,都会降低温度,使得接受较差解的可能性变小。在走的过程中,更新最优解的值。

对这个题目来说,从空间中某一个点出发,如果每次都找离当前点最远的点,往那个点的方向走,大概率可以使结果变得更优。

随便设了个温度下降速率为\(0.97\),一遍就AC了。参数都不用调。

#include <bits/stdc++.h>

#define FOPI freopen("in.txt", "r", stdin)
#define FOPO freopen("out.txt", "w", stdout)

using namespace std;
typedef long long LL;
const int maxn = 100 + 5;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const double start_T = 1000;

struct Point
{
    double x, y, z;
    Point() {}
    Point(int _x, int _y, int _z):x(_x), y(_y), z(_z) {}
}a[maxn];

double dist(Point a, Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + (a.z-b.z)*(a.z-b.z));
}

int n;
double SA()
{
    Point p = Point(0,0,0);
    int s = 0;
    double ans = inf, rate = 0.97, T = start_T;
    while(T > eps)
    {
        for (int i = 1; i <= n; i++)
            if(dist(p, a[i]) > dist(p, a[s])) s = i;
        double d = dist(p, a[s]);
        ans = min(ans, d);
        p.x += (a[s].x - p.x) * T / start_T;
        p.y += (a[s].y - p.y) * T / start_T;
        p.z += (a[s].z - p.z) * T / start_T;
        T *= rate;
    }
    return ans;
}

int main()
{
//    FOPI;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].z);
    double ans = SA();
    printf("%.8f\n", ans);
}

posted @ 2019-05-23 10:37  jvruodejrLS  阅读(272)  评论(0编辑  收藏  举报

Contact with me