码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-02-07 22:45:51      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:clu   ==   www   its   pre   down   std   概率   一个   

简介

就是模拟退火的物理过程,每次随机逼近乘上温度,以\(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)

标签:clu   ==   www   its   pre   down   std   概率   一个   

原文地址:https://www.cnblogs.com/cjoieryl/p/8428469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!