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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| #include <cstdio> #include <queue> using namespace std; typedef long long LL; typedef pair<LL, int> PLI; const int N = 1e5 + 10, M = 1e7 + 10; const LL inf = 1e14 + 1; int idx, hd[N << 3], nxt[M], edg[M]; LL d[N << 3], wt[M]; int n, m, st, id[N]; bool vis[N << 3]; void add(int a, int b, LL c) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; wt[idx] = c; } struct Node { int l, r; }; struct Seg { Node tr[N << 2]; void build(int x, int l, int r, bool op) { tr[x].l = l; tr[x].r = r; if (l == r) { id[l] = x; return; } int mid = l + r >> 1; build(x << 1, l, mid, op); build(x << 1 | 1, mid + 1, r, op); if (op) { add((x << 1) + (n << 2), x + (n << 2), 0); add((x << 1 | 1) + (n << 2), x + (n << 2), 0); } else { add(x, x << 1, 0); add(x, x << 1 | 1, 0); } } void addRange(int x, int pos, int l, int r, LL w, bool op) { if (tr[x].l >= l && tr[x].r <= r) { if (op) add(x + (n << 2), id[pos], w); else add(id[pos] + (n << 2), x, w); return; } int mid = tr[x].l + tr[x].r >> 1; if (l <= mid) addRange(x << 1, pos, l, r, w, op); if (r > mid) addRange(x << 1 | 1, pos, l, r, w, op); } } A, B; int main() { scanf("%d%d%d", &n, &m, &st); for (int i = 1; i <= (n << 3); i++) hd[i] = -1; A.build(1, 1, n, 0); B.build(1, 1, n, 1); for (int i = 1; i <= n; i++) add(id[i], id[i] + (n << 2), 0); for (int op; m; m--) { scanf("%d", &op); if (op == 1) { int u, v; LL w; scanf("%d%d%lld", &u, &v, &w); add(id[u], id[v], w); } else if (op == 2) { int u, l, r; LL w; scanf("%d%d%d%lld", &u, &l, &r, &w); A.addRange(1, u, l, r, w, 0); } else if (op == 3) { int u, l, r; LL w; scanf("%d%d%d%lld", &u, &l, &r, &w); B.addRange(1, u, l, r, w, 1); } } for (int i = 1; i <= (n << 3); i++) d[i] = inf; d[id[st]] = 0; priority_queue<PLI, vector<PLI>, greater<PLI>> q; q.push(make_pair(0, id[st])); while (!q.empty()) { int t = q.top().second; q.pop(); if (vis[t]) continue; vis[t] = true; for (int i = hd[t]; ~i; i = nxt[i]) if (d[t] + wt[i] < d[edg[i]]) { d[edg[i]] = d[t] + wt[i]; q.push(make_pair(d[edg[i]], edg[i])); } } for (int i = 1; i <= n; i++) if (d[id[i]] == inf) printf("-1 "); else printf("%lld ", d[id[i]]); return 0; }
|