zoukankan      html  css  js  c++  java
  • 洛谷 P1337 [JSOI2004]平衡点 / 吊打XXX

    模拟退火模板题,我们的目标是minimize$$sum^n_{i=1}dis[i]*weight[i]$$

    其中$$dis[i]=distance(P_{ans},P_{i})$$

    故相当于我们要在一个空间内找一个点$P_{ans}$,具有最优性质

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<iostream>
    using namespace std;
    class HOLE {
    public:
        void Set(int _x, int _y, int _w) {
            x = _x;
            y = _y;
            w = _w;
        }
        double Wg(double X, double Y) {
            return sqrt((X - x) * (X - x) + (Y - y) * (Y - y)) * w;
        }
    private:
        double x, y, w;
    };
    HOLE hole[1010];
    double cal_val(double x, double y, int n) {
        double ret = 0;
        for (int i = 1; i <= n; i++) {
            ret += hole[i].Wg(x, y);
        }
        return ret;
    }
    void sa(double& x0, double& y0, int n) {
        double Temperature = 5000;
        double zeroT = 1e-15;
        double down_rate = 0.004;
        double ansval = cal_val(x0, y0, n);
        int cnt = 0;
        while (Temperature > zeroT) {
            double newx = x0 + (rand() * 2 - RAND_MAX) * Temperature;
            double newy = y0 + (rand() * 2 - RAND_MAX) * Temperature;
            double newval = cal_val(newx, newy, n);
            double delta_val = newval - ansval;
            if (delta_val < 0) {
                x0 = newx;
                y0 = newy;
                ansval = newval;
            }
            else if (rand() < exp(-delta_val / Temperature) * RAND_MAX) {
                x0 = newx;
                y0 = newy;
            }
            Temperature *= 1 - down_rate;
        }
    }
    int main() {
        srand(623223771);
        int n;
        cin >> n;
        double ansx = 0, ansy = 0;
        for (int i = 1; i <= n; i++) {
            int x, y, w;
            cin >> x >> y >> w;
            hole[i].Set(x, y, w);
            ansx += x;
            ansy += y;
        }
        ansx /= n;
        ansy /= n;
        for (int i = 1; i <= 5; i++) {
            sa(ansx, ansy, n);
        }
        printf("%.3lf %.3lf
    ", ansx, ansy);
        return 0;
    }
  • 相关阅读:
    iOS 网络优化--页面返回的时候取消网络请求
    iOS 内存管理
    realm数据库使用
    KNN 算法分类电影类型
    sklearn库学习之01
    Python 生成4位验证码图片
    Python——读写Excel文件
    KNN--用于手写数字识别
    Python基础学习-'module' object has no attribute 'urlopen'解决方法
    swift_通知的使用
  • 原文地址:https://www.cnblogs.com/halfrot/p/14821864.html
Copyright © 2011-2022 走看看