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

    题目大意:有$n$个重物,每个重物系在一条绳子上。所有绳子系在一起,问绳结最终平衡于何处。

    题解:$NOIP$前学学模拟退火,但发现我脸好黑啊。。。

    卡点:脸黑

    C++ Code:

    #include <cstdio>
    #include <cmath>
    #include <ctime>
    #include <cstdlib>
    #include <ctime>
    #define maxn 1010
    int Tim = 22;
    const int __Tim = 2000;
    const double eps = 1e-6, __eps = 1e-4;
    const double k = 0.995, Temp = 500;
    inline double abs(double a) {return a < 0 ? -a : a;}
    inline double dis(double x, double y) {return sqrt(x * x + y * y);}
    inline double RAND() {return static_cast<double> (rand()) / RAND_MAX;}
    
    int n;
    struct point {
    	double x, y, w;
    	inline void operator += (const point &rhs) {
    		x += rhs.x;
    		y += rhs.y;
    		w += rhs.w;
    	}
    	inline void operator /= (const double &rhs) {
    		x /= rhs;
    		y /= rhs;
    	}
    } s[maxn], ans;
    
    inline double calc(point &lhs) {
    	lhs.w = 0;
    	for (register int i = 1; i <= n; i++) {
    		lhs.w += dis(lhs.x - s[i].x, lhs.y - s[i].y) * s[i].w;
    	}
    	return lhs.w;
    }
    void SA() {
    	double T = Temp, del;
    	point now = ans, nxt;
    	while (T > eps) {
    		nxt.x = now.x + T * (2 * RAND() - 1);
    		nxt.y = now.y + T * (2 * RAND() - 1);
    		del = calc(nxt);
    		if ((del < now.w) || exp((now.w - nxt.w) / T) > RAND()) now = nxt;
    		if (del < ans.w) ans = nxt;
    		T *= k;
    	}
    	int Tim = __Tim;
    	now = ans;
    	T = __eps;
    	while (Tim --> 0) {
    		nxt.x = now.x + T * (2 * RAND() - 1);
    		nxt.y = now.y + T * (2 * RAND() - 1);
    		del = calc(nxt);
    		if ((del < now.w) || exp((now.w - nxt.w) / T) > RAND()) now = nxt;
    		if (del < ans.w) ans = nxt;
    	}
    }
    
    int main() {
    	srand(20040826);
    	scanf("%d", &n);
    	for (int i = 1; i <= n; i++) {
    		scanf("%lf%lf%lf", &s[i].x, &s[i].y, &s[i].w);
    		ans += s[i];
    	}
    	ans /= n; calc(ans);
    	while (Tim --> 0) SA();
    	printf("%.3lf %.3lf
    ", ans.x, ans.y);
    	return 0;
    }
    

      

  • 相关阅读:
    java文件下载
    java程序运行原理
    java io流(核心:读进来,写出去)
    oracle操作表和字段的sql复习
    深入理解C/S和B/S模式
    Windows PyCharm永久激活
    MacBook PyCharm永久激活
    百度云同同步盘 mac版
    SJW-遍历我的账户左侧导航页面(句柄切换)
    python-selenium无法调用浏览器的问题==
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9920406.html
Copyright © 2011-2022 走看看