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
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int, int> pii; const int inf = 0x3f3f3f3f, N = 20000, M = 1e7 + 10; struct edge { int to, next; ll w; } e[M]; int head[N], idx, cur[N]; int dist[N], s, t, tn; bool vis[N];
void init() { idx = 0; for(int i = 1; i <= tn; i++) head[i] = -1; }
void _add(int a, int b, ll c) { e[idx] = {b, head[a], c}; head[a] = idx++; }
void add(int a, int b, ll c, ll re = 0){ _add(a, b, c); _add(b, a, re); }
bool bfs() { for (int i = 1; i <= tn; i++) vis[i] = false; queue<int> q; q.push(s); vis[s] = true; dist[s] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = head[x]; i != -1; i = e[i].next) { int to = e[i].to; ll w = e[i].w; if (!vis[to] && w) { vis[to] = true; dist[to] = dist[x] + 1; q.push(to); } } } return vis[t]; }
ll dfs(int x, ll flow) { if (x == t || !flow) return flow; ll delta = 0, f; for (int i = cur[x]; i != -1; i = e[i].next) { int to = e[i].to; ll w = e[i].w; cur[x] = i; if (dist[to] == dist[x] + 1 && (f = dfs(to, min(flow, w))) > 0) { e[i].w -= f; e[i ^ 1].w += f; flow -= f; delta += f; if (flow == 0) break; } } return delta; }
ll MaxFlow() { ll ans = 0; while (bfs()) { for (int i = 1; i <= tn; i++) cur[i] = head[i]; ans += dfs(s, inf); } return ans; }
int a[N], prv[N];
void solve() { int n, m, k, tot = 0; cin >> n >> m >> k; s = 2 * m + 1; t = tn = s + 1; init(); for(int i = 1; i <= n; i++){ cin >> a[i]; prv[i] = s; } for(int i = 0, u, v; i < m; i++){ cin >> u >> v; if(prv[u] == s) add(prv[u], tot + 1, 1); else add(prv[u], tot + 1, a[u]); prv[u] = ++tot; if(prv[v] == s) add(prv[v], tot + 1, 1); else add(prv[v], tot + 1, a[v]); prv[v] = ++tot; add(prv[u], prv[v], 1, 1); } for(int i = 0, x; i < k; i++){ cin >> x; add(prv[x], t, a[x]); } cout << MaxFlow() << '\n'; }
int main() { int T = 1; ios::sync_with_stdio(false); cin >> T; while (T--) solve(); return 0; }
|