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
|
#define maxn 5010 #define maxm 50100 #define inf 0x3f3f3f3f using namespace std;
struct Edge { int v, c, next; Edge(int v, int c, int next): v(v), c(c), next(next) {} Edge(){} }e[maxm * 6 + maxn * 2]; int p[maxn + maxm]; int cnt, n, m, T;
void init() { cnt = 0; memset(p, -1, sizeof(p)); }
void insert(int u, int v, int c) { e[cnt] = Edge(v, c, p[u]); p[u] = cnt++; }
int d[maxn + maxm]; bool bfs() { memset(d, -1, sizeof(d)); queue<int> q; d[0] = 0; q.push(0); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = p[u]; i != -1; i = e[i].next) { int v = e[i].v; if(e[i].c > 0 && d[v] == -1){ d[v] = d[u] + 1; q.push(v); } } } return d[T] != -1; }
int dfs(int u, int flow){ if(u == T) return flow; int res = 0; for(int i = p[u]; i != -1; i = e[i].next){ int v = e[i].v; if(e[i].c > 0 && d[v] == d[u] + 1){ int tmp = dfs(v, min(flow, e[i].c)); e[i].c -= tmp; flow -= tmp; e[i^1].c += tmp; res += tmp; if(flow == 0) break; } } if(res == 0) d[u] = -1; return res; }
int dinic() { int res = 0; while(bfs()){ res += dfs(0, inf); } return res; }
int main() { init(); int p, a, b, c, sum = 0; scanf("%d%d", &n, &m); T = n + m + 1; for(int i = 1; i <= n; i++){ scanf("%d", &p); insert(i + m, T, p); insert(T, i + m, 0); } for(int i = 1; i <= m; i++){ scanf("%d%d%d", &a, &b, &c); sum += c; insert(i, a + m, inf); insert(a + m, i, 0); insert(i, b + m, inf); insert(b + m, i, 0); insert(0, i, c); insert(i, 0, 0); } printf("%d\n", sum - dinic()); return 0;
|