# 最大權閉合子圖_洛谷3410

2 3
10 1 2 0
25 2 3 0
5 6 7

17

1.記得給flow 賦值，即while(flow = dinic(s, inf)) 。
AC代碼：

``````#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int N = 300, M = 30000;
const int inf = 0x3f3f3f3f;

int n, m, ans, maxflow, s, t;
int h[N], w[M], e[M], ne[M], now[M], idx;
int d[N];

inline void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, w[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}

inline bool bfs(void) {
queue<int> q;
while(q.size()) q.pop();
memset(d, 0, sizeof d);

d[s] = 1; q.push(s);

while(q.size()) {
int u = q.front(); q.pop();

for(int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
if(w[i] && !d[v]) {
d[v] = d[u] + 1;
q.push(v);
}
}
}

if(d[t] == 0) return false;
return true;

}

inline int dinic(int u, int flow) {
int i;

if(u == t) return flow;

for(i = now[u]; i != -1; i = ne[i]) {
int v = e[i];
if(d[v] == d[u] + 1 && w[i]) {
int k = dinic(v, min(flow, w[i]));
if(k) {
w[i] -= k;
w[i ^ 1] += k;
return k;
} else d[v] = 0;
}

now[u] = i;
}

return 0;
}

int main(void) {
//	freopen("in.txt", "r", stdin);

scanf("%d%d", &m, &n);
memset(h, -1, sizeof h);
s = m + n + 1, t = s + 1;

for(int i = 1; i <= m; i ++) {
int cost; int b;
scanf("%d", &cost); ans += cost;

while(scanf("%d", &b) != EOF && b) {
}
}

for(int i = 1; i <= n; i ++) {
int cost; scanf("%d", &cost);
}

int flow;
while(bfs()) {
memcpy(now, h, sizeof h);
while(flow = dinic(s, inf)) maxflow += flow;
}

ans -= maxflow;

printf("%d\n", ans);

//	fclose(stdin);
return 0;
}
``````