zoukankan      html  css  js  c++  java
  • 洛谷 [P1337] 平衡点

    模拟退火练手

    一道模拟退火的好题
    结果一定势能最小
    与模拟退火思路高度一致

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    #include <cmath>
    #include <ctime>
    using namespace std;
    const int MAXN = 1005;
    struct point {
    	double x, y, wei;
    }poor[MAXN];
    int n;
    const double delta = 0.993;
    const double eps = 1e-14;
    double xans, yans, ans = 1e15, best = 1e15;
    double energy(double x, double y) {
    	double tot = 0.0;
    	for(int i = 1; i <= n; i++) {
    		tot += sqrt((poor[i].x - x) * (poor[i].x - x) + (poor[i].y - y) * (poor[i].y - y)) * poor[i].wei;
    	}
    	return tot;
    }
    double RAND(double T) {
    	return (rand() * 2 - RAND_MAX) * T;
    }
    void solve() {
    	double T = 10000.0;
    	best = ans = energy(xans, yans);
    	double xx = xans, yy = yans;
    	while(T > eps) {
    		double xt = xx + RAND(T);
    		double yt = yy + RAND(T);
    		double anst = energy(xt, yt);
    		double DE = ans - anst;
    		if(DE > 0.0) {
    			xx = xt; yy = yt;
    			ans = anst;
    			if(ans < best) {
    				xans = xx; yans = yy;
    				best = ans;
    			}
    		} else if(exp(DE / T) * RAND_MAX > (double)rand()) {
    			xx = xt; yy = yt;ans = anst;
    		}
    		T *= delta;
    	}
    }
    int main() {
    	srand(time(NULL));
    	cin >> n;
    	for(int i = 1; i <= n; i++) {
    		cin >> poor[i].x >> poor[i].y >> poor[i].wei;
    		xans += poor[i].x; yans += poor[i].y;
    	}
    	xans /= (double)n; yans /= (double)n;
    	solve();
    	solve();
    	solve();
    	printf("%.3f %.3f
    ", xans, yans);
    	return 0;
    }
    
  • 相关阅读:
    今天的进度又慢了
    继续还有一些基本功能
    没什么事情
    今天好冷啊
    估计下周一就不去了
    再次出发
    诡异的php curl error Empty reply from server
    postgresql interval 字段拼接
    使用root用户通过SSH登录Linux实例时报“Permission denied, please try again”的错误
    pgsql 记录类型
  • 原文地址:https://www.cnblogs.com/Mr-WolframsMgcBox/p/9928919.html
Copyright © 2011-2022 走看看