# P2805 [NOI2009]植物大战僵尸

### 最大权闭合子图

$G$ 中的点有点权，则点权和最大的闭合子图称为有向图 $G$最大权闭合子图

#### 定理

• 最大权闭合图的点权和 $=$ 所有正权点权值和 $–$ 最小割。
• 上述图的最小割包含 $S$不在最大权闭合图内的正权节点的边和在最大权闭合图内的负权节点$T$ 的边。

#### 本题题解

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 6, M = 1e6 + 6, inf = 1e9;
int n, m, s, t, a[N], ans, d[N], deg[N], v[N];
int Head[N], Edge[M], Leng[M], Next[M], tot = 1;
queue<int> q;
vector<int> e[N];

inline void add(int x, int y, int z) {
Edge[++tot] = y;
Leng[tot] = z;
}

inline bool bfs() {
memset(d, 0, sizeof(d));
queue<int> q;
q.push(s);
d[s] = 1;
while (q.size()) {
int x = q.front();
q.pop();
for (int i = Head[x]; i; i = Next[i]) {
int y = Edge[i], z = Leng[i];
if (deg[y] || d[y] || !z) continue;
q.push(y);
d[y] = d[x] + 1;
if (y == t) return 1;
}
}
return 0;
}

int dinic(int x, int flow) {
if (x == t) return flow;
int rest = flow;
for (int i = Head[x]; i && rest; i = Next[i]) {
int y = Edge[i], z = Leng[i];
if (d[y] != d[x] + 1 || !z) continue;
int k = dinic(y, min(rest, z));
if (!k) d[y] = 0;
else {
Leng[i] -= k;
Leng[i^1] += k;
rest -= k;
}
}
return flow - rest;
}

int main() {
cin >> n >> m;
s = n * m, t = s + 1;
for (int i = 0; i < s; i++) {
scanf("%d", &a[i]);
int k;
scanf("%d", &k);
while (k--) {
int x, y;
scanf("%d %d", &x, &y);
e[i].push_back(x * m + y);
++deg[x*m+y];
}
}
for (int i = 0; i < n; i++)
for (int j = 1; j < m; j++) {
e[i*m+j].push_back(i * m + j - 1);
++deg[i*m+j-1];
}
for (int i = 0; i < s; i++)
if (!deg[i]) q.push(i), v[i] = 1;
while (q.size()) {
int x = q.front();
q.pop();
for (unsigned int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if (!v[y] && !--deg[y]) q.push(y), v[y] = 1;
}
}
for (int x = 0; x < s; x++) {
if (!v[x]) continue;
for (unsigned int i = 0; i < e[x].size(); i++) {
int y = e[x][i];
if (!v[y]) continue;
}