模拟退火小结(Bzoj3680:吊打xxx)

简介

就是模拟退火的物理过程,每次随机逼近乘上温度,以\(e^{\Delta/T}\)的概率接受答案,随机一个概率比较
然后就是调参+乱搞

题目

Bzoj3680:吊打xxx
代码

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1005);

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

const double EPS(1e-15);
const double dt(0.99);

double x[_], y[_], w[_], ans, ansx, ansy;
int n;

# define Sqr(x) ((x) * (x))
IL double Dis(RG double x1, RG double y1, RG double x2, RG double y2){
    return sqrt(Sqr(x1 - x2) + Sqr(y1 - y2));
}

IL double Sum(RG double x0, RG double y0){
    RG double ret = 0;
    for(RG int i = 1; i <= n; ++i) ret += Dis(x[i], y[i], x0, y0) * w[i];
    return ret;
}

IL double SA(RG double T){
    RG double x0 = ansx, y0 = ansy, cnt = 1e18;
    for(; T > EPS; T *= dt){
        RG double dx = rand() * 2 - RAND_MAX, dy = rand() * 2 - RAND_MAX;
        RG double xx = x0 + T * dx;
        RG double yy = y0 + T * dy;
        RG double ret = Sum(xx, yy);
        if(ret < ans) ans = ret, ansx = xx, ansy = yy;
        if(ret < cnt || exp((cnt - ret) / T) * RAND_MAX > rand()){
            cnt = ret;
            x0 = xx, y0 = yy;
        }
    }
}

int main(RG int argc, RG char* argv[]){
    n = Input();
    srand(n + 19260817); RG double x0 = 0, y0 = 0;
    for(RG int i = 1; i <= n; ++i) x[i] = Input(), y[i] = Input(), w[i] = Input();
    for(RG int i = 1; i <= n; ++i) x0 += x[i], y0 += y[i];
    x0 /= n; y0 /= n; ansx = x0; ansy = y0;
    ans = Sum(x0, y0);
	SA(100000);
    printf("%.3lf %.3lf\n", ansx, ansy);
    return 0;
}
posted @ 2018-02-07 21:12  Cyhlnj  阅读(149)  评论(0编辑  收藏  举报