90pts MLE 求优化
查看原帖
90pts MLE 求优化
689810
2020luke楼主2024/3/18 20:11

rt

#include<bits/stdc++.h>
#define PII pair<int,int>
#define fir first
#define sec second
using namespace std;
const int N = 55;
int n, m, k, a, b, f[N];
vector< PII > g[N], g1[N];
struct node {
	int u, d;
	bool operator < (const node &rhs) const {
		return rhs.d < d;
	}
};
struct node2 {
	int u, d;
	vector< int > v;
	long long st;
	bool operator < (const node2 &rhs) const {
		if(rhs.d + f[rhs.u] != d + f[u]) return rhs.d + f[rhs.u] < d + f[u];
		else return rhs.v < v;
	}
};
void dij() {
	memset(f, 0x3f, sizeof(f));
	f[b] = 0;
	priority_queue< node > q;
	q.push({b, 0});
	while(!q.empty()) {
		int u = q.top().u, d = q.top().d;
		q.pop();
		if(f[u] < d) continue;
		for(auto it : g1[u]) {
			int v = it.fir, w = it.sec;
			if(d + w < f[v]) {
				f[v] = d + w;
				q.push({v, d + w});
			}
		}
	}
}
void a_star() {
	priority_queue< node2 > q;
	q.push({a, 0, {a}, (1ll << a)});
	int cnt = 0;
	while(!q.empty()) {
		int u = q.top().u, d = q.top().d;
		vector< int > vt = q.top().v;
		long long st = q.top().st;
		if(u == b) cnt++;
		if(cnt == k) {
			cout << vt[0];
			for(int i = 1; i < vt.size(); i++) {
				cout << '-' << vt[i];
			}
			exit(0);
		}
		q.pop();
		for(auto it : g[u]) {
			int v = it.fir, w = it.sec;
			if(!((st >> v) & 1)) {
				vt.push_back(v);
				st |= (1ll << v);
				q.push({v, d + w, vt, st});
				st ^= (1ll << v);
				vt.pop_back();
			}
		}
	}
}
int main() {
	cin >> n >> m >> k >> a >> b;
	for(int i = 1, u, v, w; i <= m; i++) {
		cin >> u >> v >> w;
		g[u].push_back({v, w});
		g1[v].push_back({u, w});
	}
	for(int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end(), [](PII x, PII y) {
		return x.fir < y.fir;
	});
	dij();
	a_star();
	cout << "No";
	return 0;
}
2024/3/18 20:11
加载中...