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
| #include <cstdio> #include <queue> #include <vector> #define pb push_back #define fi first #define se second using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef pair <int, int> PII; const int N = 5e3 + 10, M = 2e5 + 10, T = 1e7 + 10, inf = 1e9; vector <int> e[N]; bool vis[N]; int n, m, K, st, ed, d[N], pre[N]; int idx, hd[N], nxt[M << 1], edg[M << 1], wt[M << 1]; priority_queue <PII, vector <PII>, greater<PII> > q; struct Node { int l, r, w, d, ed; void init (int _w, int _ed) { w = _w, ed = _ed, d = 1; } } tr[T]; int tot, rt[N]; int merge (int x, int y) { if (!x || !y) return x | y; if (tr[x].w > tr[y].w) swap(x, y); tr[++tot] = tr[x], x = tot; tr[x].r = merge(tr[x].r, y); if (tr[tr[x].r].d > tr[tr[x].l].d) swap(tr[x].l, tr[x].r); tr[x].d = tr[tr[x].r].d + 1; return x; } void dfs (int u) { for (int v : e[u]) rt[v] = merge(rt[v], rt[u]), dfs(v); } void add (int a, int b, int c) { nxt[idx] = hd[a], hd[a] = idx, edg[idx] = b, wt[idx] = c, ++idx; } int main () { read(n, m, K, st, ed); for (int i = 1; i <= n; ++i) hd[i] = -1; for (int a, b, c; m; --m) { read(a, b, c); if (a ^ ed) add(a, b, c), add(b, a, c); } for (int i = 1; i <= n; ++i) d[i] = inf; d[ed] = 0; q.push({d[ed], ed}); while (!q.empty()) { int t = q.top().se; q.pop(); if (vis[t]) continue; vis[t] = true; for (int i = hd[t]; ~i; i = nxt[i]) if (i & 1) if (d[t] + wt[i] < d[edg[i]]) { pre[edg[i]] = i; d[edg[i]] = d[t] + wt[i]; q.push({d[edg[i]], edg[i]}); } } for (int i = 1; i <= n; ++i) if (i ^ ed) e[edg[pre[i] ^ 1]].pb(i); for (int i = 1; i < idx; i += 2) if (pre[edg[i]] ^ i) { tr[++tot].init(wt[i] + d[edg[i ^ 1]] - d[edg[i]], edg[i ^ 1]); rt[edg[i]] = merge(rt[edg[i]], tot); } dfs(ed); if (rt[st]) q.push({tr[rt[st]].w, rt[st]}); while (K--) { if (q.empty()) return puts("-1"), 0; int k = q.top().fi, t = q.top().se; q.pop(); write(k + d[st]), puts(""); if (tr[t].l) q.push({tr[tr[t].l].w + k - tr[t].w, tr[t].l}); if (tr[t].r) q.push({tr[tr[t].r].w + k - tr[t].w, tr[t].r}); if (rt[tr[t].ed]) q.push({k + tr[rt[tr[t].ed]].w, rt[tr[t].ed]}); } return 0; }
|