能过吃奶酪,但是写寄了
查看原帖
能过吃奶酪,但是写寄了
482610
Mortidesperatslav楼主2024/5/20 20:31
#include<bits/stdc++.h>
using namespace std;
double T0 = 1e5, d = 0.988, Tk = 1e-3;
int Ans = INT_MAX;
double Exp(double x){
	return pow(2.71828, x);
}
vector<int> G[20];
int ans[20], nans[20];
int n;
int calc(){
	int res = 0;
	for (int i = 1; i <= n; i++)
		for (auto v : G[ans[i]])
			if (v > ans[i])
				for (int j = 1; j <= n; j++)
					if (ans[j] == v)
						res += abs(j - i);
	return res;
}
void th(){
	double T = T0;
	while (T > Tk){
		for (int i = 1; i <= n; i++)
			nans[i] = ans[i];
		int lans = calc();
		int sx = rand() % n + 1, sy = rand() % n + 1;
		swap(nans[sx], nans[sy]);
		int Nans = calc();
		if (Nans < lans)
			swap(ans[sx], ans[sy]);
		else{
			double gl = Exp(-(Nans - lans) / T);
			if ((double)rand() / (double)RAND_MAX < gl)
				swap(ans[sx], ans[sy]);
		}
		T *= d;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    int st = clock();
	srand(time(0) * 11451);
	srand(rand());
	cin >> n;
	for (int i = 1; i <= n; i++){
		int u, v, w;
		cin >> u >> v >> w;
		G[i].push_back(u);
		G[i].push_back(v);
		G[i].push_back(w);
	}
	for (int i = 1; i <= n; i++)
		ans[i] = i;
	while (clock() - st < 0.95 * CLOCKS_PER_SEC){
	    th();
	    for (int i = 1; i <= n; i++)
	    	nans[i] = ans[i];
	   // cerr << calc() << "\n"; 
	    Ans = min(Ans, calc());
	}
	cout << Ans;
}

在 4949 分和 5858 分之间波动,为什么呢?

2024/5/20 20:31
加载中...