您的位置:首页 > 其它

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

2015-04-17 19:38 489 查看
每次随机改变当前中心坐标 然后退火 退火完成后在周围附近在搜索几次(没这个我一直过不了样例)

#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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: