模拟退火

#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const double delta = 0.996;
const double eps = 1e-15;

double X, Y;

struct Point {
    double x, y;
} a[1005], ans;
int n;
double ans_val = 0;

double Min_Val(double x, double y) {
    double res = X * X + Y * Y;
    for (int i = 1; i <= n; i++) {
        double dx = x - a[i].x;
        double dy = y - a[i].y;
        res = min(res, sqrt(dx * dx + dy * dy));
    }
    return res;
}

inline double Rand() {
    return 2 * rand() - RAND_MAX;
}

inline void Solve() {
    double T = 10000000;
    while (T > eps) {
        Point tmp = {ans.x + Rand() * T, ans.y + Rand() * T};
        if (!(0 <= tmp.x && tmp.x <= X && 0 <= tmp.y && tmp.y <= Y)) {
            T *= delta;
            continue;
        }
        double tmp_val = Min_Val(tmp.x, tmp.y);
        if (ans_val < tmp_val)ans = tmp, ans_val = tmp_val;
        else if (exp((tmp_val - ans_val) / T) * RAND_MAX > rand())ans = tmp;
        T *= delta;
    }
}

signed main() {
//    freopen("in", "r", stdin), freopen("out", "w", stdout);
    int Ts;
    scanf("%d", &Ts);
    while (Ts--) {
        scanf("%lf %lf %d", &X, &Y, &n);
        ans = {0, 0};
        for (int i = 1; i <= n; i++) {
            scanf("%lf %lf", &a[i].x, &a[i].y);
            ans.x += a[i].x, ans.y += a[i].y;
        }
        ans.x /= n, ans.y /= n;
        ans_val = Min_Val(ans.x, ans.y);
        for (int i = 1; i <= 10; i++)Solve();
        printf("The safest point is (%.1lf, %.1lf).\n", ans.x, ans.y);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值