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

Posted Cyhlnj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟退火小结(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((ret - ans) / 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);
    for(RG int i = 1; i <= 10; ++i) SA(1000000);
    printf("%.3lf %.3lf\n", ansx, ansy);
    return 0;
}

以上是关于模拟退火小结(Bzoj3680:吊打xxx)的主要内容,如果未能解决你的问题,请参考以下文章

bzoj3680: 吊打XXX(模拟退火)

BZOJ 3680 吊打XXX(模拟退火)

BZOJ3680吊打XXX 模拟退火

BZOJ3680吊打xxx [模拟退火]

BZOJ 3680: 吊打XXX模拟退火算法裸题学习,爬山算法学习

[bzoj 3680]吊打XXX