bzoj3680

$模拟退火$

$这种全局最优的问题用模拟退火$

$模拟退火就是每次向四周随机移动,移动的幅度和温度成正比,如果新的位置更优就接受,否则按一定概率接收,概率和温度成正比$

$最后稳定后再在最优解附近蹦跶几下看看有没有更好的$

$你问我这是什么道理,我说无(我)可(不)奉(知)告(道)$

#include<bits/stdc++.h>
using namespace std;
const int N = 10005;
struct P {
    double x, y, w;
} p[N], ans;
int n;
double mn = 1e18, T = 100000;
double rd() {
    return rand() % 10000 / 10000.0; 
}
double sqr(double x) {
    return x * x;
}
double calc(P a) {
    double ret = 0;
    for(int i = 1; i <= n; ++i) {
        ret += sqrt(sqr(a.x - p[i].x) + sqr(a.y - p[i].y)) * p[i].w;
    }
    if(ret < mn) {
        mn = ret;
        ans = a;
    }
    return ret;
}
int main() {
    srand(19992147);
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].w);
        ans.x += p[i].x;
        ans.y += p[i].y;
    }
    ans.x /= n;
    ans.y /= n;
    P now = ans;
    while(T > 0.001) {
        P nw;
        nw.x = now.x + T * (rd() * 2 - 1.0); 
        nw.y = now.y + T * (rd() * 2 - 1.0);
        double d = calc(now) - calc(nw);
        if(d > 0 || exp(d / T) > rd()) {
            now = nw;
        } 
        T *= 0.991;
    }
    for(int i = 1; i <= 1000; ++i) {
        P nw;
        nw.x = ans.x + T * (rd() * 2 - 1.0);
        nw.y = ans.y + T * (rd() * 2 - 1.0);
        calc(nw);
    }
    printf("%.3f %.3f\n", ans.x, ans.y);
    return 0;
}
View Code

 

posted @ 2018-01-28 22:31  19992147  阅读(105)  评论(0编辑  收藏  举报