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

简介

就是模拟退火的物理过程,每次随机逼近乘上温度,以 eΔ/T e Δ / 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值