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
| #pragma G++ optimize (3)
#include <cstdio> #include <cstring> #include <queue>
using std::priority_queue;
const int ELAS = 1<<14; static char buf[ELAS],*p1=buf,*p2=buf; inline char gc() { return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,ELAS,stdin),p1==p2)?EOF:*p1++; } inline int read () { register int x = 0; register char c = gc(); register bool m = false; while ((c < '0' || c > '9') && (c ^ '-')) c = gc(); if (c == '-') m = true, c = gc (); while (c>='0'&&c<='9') x = (x<<3) + (x<<1) + (c^'0'), c = gc(); return m? -x:x; } static void write (int x) { if (x > 9) write (x / 10); putchar (x % 10 + 48); } static void nwrite (int x) {write (x); putchar (' ');}
#define maxn 100001 #define maxm 200001 #define Re register
struct node { int id, val; friend bool operator < (node a, node b) { return a.val > b.val; } };
priority_queue <node> que; int head[maxn], wei[maxm], too[maxm], nxt[maxm], ecnt; bool vis[maxn]; int dis[maxn];
signed main () { register int n = read (), m = read (), sta = read (); register int fr, to, va; while (m --) { fr = read (), nxt[++ ecnt] = head[fr],\ head[fr] = ecnt, too[ecnt] = read (), wei[ecnt] = read (); } memset (dis, 0x3f3f3f3f, sizeof dis); dis[sta] = 0; que.push ({sta, 0}); while (!que.empty ()) { fr = que.top ().id, que.pop (); if (!vis[fr]) { vis[fr] = true; for (Re int i = head[fr]; i; i = nxt[i]) { to = too[i], va = wei[i]; if (dis[fr] + va < dis[to]) { dis[to] = dis[fr] + va; que.push ({to, dis[to]}); } } } } for (Re int i = 1; i <= n; ++ i) nwrite (dis[i]); return 0; }
|