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
| #include <iostream> #include <cstdio> #include <stack> #include <queue> using namespace std; const int N = 5e5 + 10, M = 5e5 + 10; namespace SCC { int n, m, w[N]; bool vis[N]; int idx, hd[N], nxt[M], edg[M]; int cnt, stmp, id[N], dfn[N], low[N]; stack<int> stk; void tarjan(int x) { dfn[x] = low[x] = ++stmp; stk.push(x); vis[x] = true; for (int i = hd[x]; ~i; i = nxt[i]) if (!dfn[edg[i]]) { tarjan(edg[i]); low[x] = min(low[x], low[edg[i]]); } else if (vis[edg[i]]) low[x] = min(low[x], dfn[edg[i]]); if (dfn[x] == low[x]) { int y; cnt++; do { y = stk.top(); stk.pop(); vis[y] = false; id[y] = cnt; } while (x != y); } } void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; } } namespace SP { bool vis[N]; int n, m; int w[N], d[N], st, p, ed[N]; int idx, hd[N], nxt[M], edg[M]; void add(int a, int b) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; } int spfa() { d[st] = w[st]; queue<int> q; q.push(st); vis[st] = true; while (!q.empty()) { int t = q.front(); q.pop(); vis[t] = false; for (int i = hd[t]; ~i; i = nxt[i]) if (d[t] + w[edg[i]] > d[edg[i]]) { d[edg[i]] = d[t] + w[edg[i]]; if (!vis[edg[i]]) { q.push(edg[i]); vis[edg[i]] = true; } } } int res = 0; for (int i = 1; i <= p; i++) res = max(res, d[ed[i]]); return res; } } int main() { using namespace SCC; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) SP::hd[i] = hd[i] = -1; for (int i = 1, a, b; i <= m; i++) { scanf("%d%d", &a, &b); add(a, b); } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); for (int i = 1; i <= n; i++) scanf("%d", &w[i]); scanf("%d%d", &SP::st, &SP::p); SP::st = id[SP::st]; for (int i = 1, a; i <= SP::p; i++) { scanf("%d", &a); SP::ed[i] = id[a]; } for (int i = 1; i <= n; i++) { SP::w[id[i]] += w[i]; for (int j = hd[i]; ~j; j = nxt[j]) if (id[i] != id[edg[j]]) SP::add(id[i], id[edg[j]]); } printf("%d", SP::spfa()); return 0; }
|