gl0526 @ 2022-04-13 16:40:24
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
int n, m, s;
vector<pii> edge[maxn];
int dist[maxn];
bool vi[maxn];
int main() {
scanf("%d %d %d", &n, &m, &s);
for (int i = 1; i <= m; ++i) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
edge[u].emplace_back(pii(v, w));
}
fill(dist + 1, dist + 1 + n, INT_MAX);
priority_queue<pii> pq;
dist[s] = 0;
pq.push(pii(0, s));
while (!pq.empty()) {
int p = pq.top().second;
pq.pop();
if (vi[p]) continue;
vi[p] = 1;
for (int i = 0; i < edge[p].size(); ++i) {
int to = edge[p][i].first;
dist[to] = min(dist[to], dist[p] + edge[p][i].second);
if (!vi[to]) pq.push(pii(dist[to], to));
}
}
for (int i = 1; i <= n; ++i) printf("%d ", dist[i]);
return 0;
}
by L_cm_C5H6 @ 2022-04-13 16:46:51
priority_queue默认大根堆 改小根堆过了
by gl0526 @ 2022-04-13 16:50:26
@L_cm_C5H6 我的,没注意,谢谢大佬