[BZOJ3680]吊打XXX && 模拟退火

每次随机改变当前中心坐标 然后退火 退火完成后在周围附近在搜索几次(没这个我一直过不了样例) 

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#define PF printf
#define SF scanf
using namespace std;
typedef long long LL;
const int MAXN = 100000;
struct Node {
    double x, y, g;
} A[MAXN+10], ans;
double Min;
int n;
inline double dis(const Node &a,const Node &b) {
    return sqrt((a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y));
}
inline double Rand() {
    return rand() % 10000 / 10000.0;
}
double calc(const Node &t) {
    double ret = 0;
    for(int i = 1; i <= n; i++) ret += dis(t, A[i]) * A[i].g;
    if(ret < Min) {
        Min = ret; ans = t;
    }
    return ret;
}
void SA(double T) {
    Node Now = ans;
    while(T > 0.001) {
        Node New;
        New.x = Now.x + T * (Rand() * 2 - 1);
        New.y = Now.y + T * (Rand() * 2 - 1);
        double dE = calc(Now) - calc(New);
        if(dE > 0 || exp(dE/T) > Rand()) Now = New;
        T *= 0.97;
    }
    for(int i = 1; i <= 125; i++) calc( (Node) { ans.x + T*(Rand()*2-1), ans.y + T*(Rand()*2-1) } );
}
int main() {
    SF("%d", &n);
    srand(n+23333);
    for(int i = 1; i <= n; i++) {
        SF("%lf%lf%lf", &A[i].x, &A[i].y, &A[i].g);
        ans.x += A[i].x; ans.y += A[i].y;
    }
    ans.x /= n; ans.y /= n;
    Min = calc(ans);
    SA(80000);
    PF("%.3f %.3f", ans.x, ans.y);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值