1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#include <bits/stdc++.h> using namespace std;
const int MAXN = 5e5+10; int n, m; int h[MAXN], e[MAXN], ne[MAXN], w[MAXN], cnt, s; void add(int a, int b, int c) { e[cnt] = b, ne[cnt] = h[a], w[cnt] = c, h[a] = cnt++; } queue<int> q; int dis[MAXN]; bool vis[MAXN]; void spfa() { memset(dis, 0x3f, sizeof dis); dis[s] = 0; q.push(s), vis[s] = 1; while (q.empty() == 0) { int u = q.front(); q.pop(); vis[u] = 0; for(int l = h[u]; l!=-1; l=ne[l]) { int v = e[l]; if((long long)dis[u]+w[l] < dis[v]) { dis[v] = dis[u]+w[l]; if(!vis[v]) { q.push(v); vis[v] = 1; } } } } } int main() { memset(h, -1, sizeof h); cin >> n >> m >> s; int a, b, c; for(int i=0; i<m; i++) { cin >> a >> b >> c; add(a, b, c); } spfa(); for(int i=1; i<=n; i++) { cout << dis[i] << " "; } cout << endl; return 0; }
|