模拟退火11pts求调
查看原帖
模拟退火11pts求调
656427
iamsh楼主2024/10/23 21:24

RT,除了 #3 都WA了,参数都是看着题解调的,样例能过,不知道为什么错了

#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
const double StartTemp = 2000;
const double EndTemp = 1e-14;
const double DeltaTemp = 0.998;
mt19937 rnd(time(0));
int n,x[N],y[N],w[N];
double ansx,ansy;
double count(double cx,double cy) {
	double ans = 0;
	for(int i = 1;i <= n;i ++) {
		double dx = cx - x[i],dy = cy - y[i];
		ans += sqrt(dx * dx + dy * dy) * w[i];
	}
	return ans;
}
void SimulateAnneal() {
	double x,y,ans = count(ansx,ansy),rlt;
	for(double temp = StartTemp;temp > EndTemp;temp *= DeltaTemp) {
		x = ansx + (rnd() - (1LL << 31)) * temp;
		y = ansy + (rnd() - (1LL << 31)) * temp;
		rlt = count(x,y);
		if(rlt - ans < 0) {
			ansx = x,ansy = y;
			ans = rlt;
		}
		else if(exp((ans - rlt) / temp) * (1LL << 31) > rnd()) {
			ansx = x,ansy = y;
		}
	}
}
int main() {
	scanf("%d",&n);
	for(int i = 1;i <= n;i ++) {
		scanf("%d%d%d",&x[i],&y[i],&w[i]);
		ansx += x[i],ansy += y[i];
	}
	ansx /= n,ansy /= n;
	while(1.0 * clock() / CLOCKS_PER_SEC < 0.95) {
		SimulateAnneal();
	}
	printf("%.3lf %.3lf",ansx,ansy);
	return 0;
}
2024/10/23 21:24
加载中...