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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e7 + 10; int head[N], rest[N], to[N], flow[N], tot = 1; void sig(int u, int v, int w) { to[++ tot] = v, flow[tot] = w, rest[tot] = head[u], head[u] = tot; } void add(int u, int v, int w) { sig(u, v, w), sig(v, u, 0); } int n, m, cnt, dis[N], S, T; ll ans; int bfs() { for(int i = 1 ; i <= cnt ; ++ i) dis[i] = -1; queue<int> q; q.push(S), dis[S] = 1; while(q.size()) { int u = q.front(); q.pop(); for(int i = head[u] ; i ; i = rest[i]) { int v = to[i]; if(dis[v] == -1 && flow[i]) { dis[v] = dis[u] + 1; q.push(v); } } } return dis[T] != -1; } int dfs(int u, int f) { if(u == T || !f) return f; int use = 0; for(int i = head[u] ; i ; i = rest[i]) { int v = to[i]; if(flow[i] && dis[v] == dis[u] + 1) { int a = dfs(v, min(f - use, flow[i])); flow[i] -= a, flow[i ^ 1] += a; use += a; if(use == f) break; } } if(!use) dis[u] = -1; return use; } int main() { scanf("%d%d", &n, &m); S = ++ cnt, T = ++ cnt; for(int i = 1, a, b ; i <= n ; ++ i) { scanf("%d%d", &a, &b); int x = ++ cnt; add(S, x, a); add(x, T, b); ans += a + b; } for(int i = 1, u, v, a, b, c ; i <= m ; ++ i) { scanf("%d%d%d%d%d", &u, &v, &b, &a, &c); u += 2, v += 2; int x = ++ cnt, y = ++ cnt; add(S, x, a); add(x, u, a); add(x, v, a); add(u, y, b); add(v, y, b); add(y, T, b); add(u, v, c); add(v, u, c); ans += a + b; } while(bfs()) { ans -= dfs(S, 0x3f3f3f3f); } printf("%lld\n", ans); }
|