1. 程式人生 > >[洛谷P1337][JSOI2004]平衡點 / 吊打XXX

[洛谷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\n", ans.x, ans.y);
	return 0;
}