堆优前向星T#1#6求助(QwQ)
查看原帖
堆优前向星T#1#6求助(QwQ)
763215
foglake楼主2023/5/12 18:49

感激不尽,蒟蒻实在做不动了。麻烦dalao们了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, maxm = 2e5 + 10;
int dis[maxn], s[maxn], w[maxm], to[maxm], e[maxm];
priority_queue <pair<int, int> > q;
int st, n, m;
void dij() {
	dis[st] = 0;
	q.push(make_pair(0, st));
	while (!q.empty()) {
		int x = q.top().second, wei = q.top().first;
		q.pop();
		if (wei != dis[x]) continue;
		for (int i = s[x]; i; i = to[i])
			if (dis[x] + w[i] < dis[e[i]]) {
				dis[e[i]] = dis[x] + w[i];
				q.push(make_pair(dis[e[i]], e[i]));
			}
	}
}
int main() {
	scanf("%d%d%d", &n, &m, &st);
	for (int i = 1; i <= n; i++) dis[i] = 1e9;
	for (int i = 1; i <= m; i++) {
		int u, v, wei;
		scanf("%d%d%d", &u, &v, &wei);
		e[i] = v, to[i] = s[u], s[u] = i, w[i] = wei;
	}
	dij();
	for (int i = 1; i <= n; i++)
		printf("%d ", dis[i]);
}
2023/5/12 18:49
加载中...