洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX

平衡点 / 吊打XXX

模拟退火入坑题


Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;
//Mystery_Sky
//
#define M 1000100
#define INF 0x3f3f3f3f
struct node {
    int x, y, w;
}point[M];
int n;
double Ans_x, Ans_y;
double t, Ans = 1e18;
int sum_x, sum_y;
const double delta = 0.993;

inline double dist(double x, double y)
{
    double sum = 0;
    for(int i = 1; i <= n; i++) {
        double dx = point[i].x - x;
        double dy = point[i].y - y;
        sum += sqrt(dx * dx + dy * dy) * point[i].w;
    }
    return sum;
}

inline void SA()
{
    double x, y;
    x = Ans_x, y = Ans_y;
    t = 2000;
    while(t > 1e-14) {
        double new_x = x + ((rand()<<1) - RAND_MAX) * t;
        double new_y = y + ((rand()<<1) - RAND_MAX) * t;
        double new_ans = dist(new_x, new_y);
        double DE = new_ans - Ans;
        if(DE < 0) {
            Ans_x = x = new_x, Ans_y = y = new_y;
            Ans = new_ans;
        }
        else if(exp(-DE/t) * RAND_MAX > rand()) x = new_x, y = new_y;
        t *= delta;
    }
}

int main() {
    srand(19260817);
    srand(rand());
    srand(rand());
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%d%d%d", &point[i].x, &point[i].y, &point[i].w);
        sum_x += point[i].x;
        sum_y += point[i].y;
    }
    Ans_x = (double) sum_x/n;
    Ans_y = (double) sum_y/n;
    SA();
    SA();
    SA();
    printf("%.3lf %.3lf\n", Ans_x, Ans_y);
    return 0;
}

转载于:https://www.cnblogs.com/Benjamin-cpp/p/11140859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值