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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <cstring>
#include <queue> #include <algorithm> using namespace std;
const int M = 200000 + 100; const int N = 100000 + 100; int hd[N], nxt[M], to[M], w[M];
int dist[N], vis[N]; int cnt;
inline void add(int x, int y, int z) { to[++cnt] = y; w[cnt] = z; nxt[cnt] = hd[x]; hd[x] = cnt; }
struct Node { int nd, val; bool operator < (const Node& b) const { return val > b.val; } };
priority_queue<Node> q;
void xhl(int beg) { memset(vis, 0, sizeof vis); memset(dist, 0x3f, sizeof dist);
dist[beg] = 0; q.push({ beg,0 });
while (q.size()) { int x = q.top().nd; q.pop();
if (vis[x]) { continue; } vis[x] = true;
for (int i = hd[x]; i; i = nxt[i]) { int y = to[i], wi = w[i]; if (dist[y] > dist[x] + wi) { dist[y] = dist[x] + wi; q.push({ y,dist[y] }); } } } }
int n, m, s;
int () { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; add(x, y, z); } xhl(s); for (int i = 1; i <= n; i++) { cout << dist[i] << " "; } cout << endl; }
|
近期评论