LP1337 [JSOI2004]平衡点 [模拟退火]

Author Avatar
空気浮遊 2018年07月19日
  • 在其它设备中阅读本文章
  • 模拟退火
  • 分享到 Facebook
  • 分享到 Telegram
  • 分享到 Twitter
  • 分享到微博

Problem

https://www.luogu.org/problemnew/show/P1337

Solution

https://www.luogu.org/blog/peng-ym/solution-p1337

这里只是提下注意的几点:

  1. 温度的变化量计入统计之中。$exp(-delta/T)$ 中的 delta 一定是正数。状态的扩展范围随温度的降低而降低。
  2. 如果不保证初始答案很靠近正确答案的话,初始温度设高。退火温度大点倒是没什么关系。
  3. 调特殊参有奇效。
  4. 善用 RAND_MAX。

Code

// Code by ajcxsu
// Problem: balance_point

#include<bits/stdc++.h>
using namespace std;

const int N=1001;
double x[N], y[N], w[N];

int n;
double f(double nx, double ny) {
    double ret=0;
    for(int i=0;i<n;i++) ret+=sqrt((x[i]-nx)*(x[i]-nx)+(y[i]-ny)*(y[i]-ny))*w[i];
    return ret;
}

int main() {
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++) cin>>x[i]>>y[i]>>w[i];
    double ax=0, ay=0, nx, ny, T=19260817, delta=0.995, eps=1e-15;
    while(T>eps) {
        nx=ax+(rand()*2-RAND_MAX)*T;
        ny=ay+(rand()*2-RAND_MAX)*T;
        double r=f(nx, ny)-f(ax, ay);
        if(r<0) ax=nx, ay=ny;
        else if(exp(-r/T)*RAND_MAX>rand()) ax=nx, ay=ny;
        T*=delta;
    }
    cout.setf(ios::fixed), cout<<setprecision(3);
    cout<<ax<<' '<<ay<<endl;
    return 0;
}

本文链接:https://pst.iorinn.moe/archives/sol-luogu-1337.html
许可: https://pst.iorinn.moe/license.html
若无特别说明,博客内的文章默认将采用 CC BY 4.0 许可协议 进行许可☆

      新篇
旧篇      
    • fingerprint Login
  • home 主页
  • inbox 归档
    • June 2026 1
    • December 2024 1
    • August 2024 1
    • June 2024 1
    • April 2024 3
    • March 2024 1
    • February 2024 2
    • January 2024 1
    • November 2023 1
    • August 2023 1
    • May 2023 1
    • February 2023 2
    • January 2023 2
    • July 2022 1
    • June 2022 1
    • April 2022 1
    • March 2022 1
    • February 2022 2
    • December 2021 1
    • November 2021 1
    • August 2021 3
    • July 2021 3
    • April 2021 2
    • March 2021 1
    • February 2021 4
    • January 2021 2
    • December 2020 1
    • November 2020 1
    • October 2020 3
    • September 2020 1
    • August 2020 1
    • July 2020 1
    • March 2020 1
    • December 2019 1
    • September 2019 1
    • July 2019 1
    • April 2019 2
    • March 2019 13
    • February 2019 15
    • January 2019 11
    • December 2018 3
    • November 2018 6
    • October 2018 28
    • September 2018 31
    • August 2018 18
    • July 2018 13
    • June 2018 27
    • May 2018 11
    • April 2018 12
    • March 2018 19
    • February 2018 8
    • January 2018 7
    • December 2017 2
    • November 2017 2
  • apps 分类
    • 笔记
    • 题解
    • 杂文
    • 技术
    • 游戏
    • 小说
  • 留言板
  • 关于
  • 友链
  • 文章总数 281
主题 - Material i
expand_less
Copyright © 2026 雪屋
Float in air.
Powered by Typecho
Theme - Material