# bzoj 3438 小M的作物

3

4 2 1

2 3 2

1

2 3 2 1 2

### Sample Output

11

1<=k< n<= 1000,0 < m < = 1000 保证所有数据及结果不超过2*10^9。

### 题解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 3030
#define M 200200
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
struct node {
int to, next, flow;
}e[M];
int tot = -1, st[N], dis[N];
void add(int x, int y, int z) {
//  printf("%d %d %d\n",x , y, z);
e[++tot].to = y;
e[tot].flow = z;
e[tot].next = st[x];
st[x] = tot;
}
void add_edge(int x, int y, int z) {
}
queue <int> que;
int bfs(int S, int T) {
memset(dis, 0, sizeof dis);
while (!que.empty()) que.pop();
que.push(S);
dis[S] = 1;
while(!que.empty()) {
int now = que.front();
que.pop();
for (int i = st[now]; ~i; i = e[i].next)
if (e[i].flow && !dis[e[i].to]) {
dis[e[i].to] = dis[now] + 1;
if (e[i].to == T) return 1;
que.push(e[i].to);
}
}
return 0;
}
int finds(int now, int T, int flow) {
if (now == T)
return flow;
int f;
for (int i = st[now]; ~i; i = e[i].next)
if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
(f = finds(e[i].to, T, min(flow, e[i].flow)))) {
e[i].flow -= f;
e[i^1].flow += f;
return f;
}
return 0;
}
int dinic(int S, int T) {
int flow = 0, x;
while(bfs(S, T)) {
while(x = finds(S, T, inf)) {
flow += x;
}
}
return flow;
}
int n, m, S, T, x, y, z, c, a[N], b[N], t, all;
main() {
memset(e, -1, sizeof e);
memset(st, -1, sizeof st);
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]), all += a[i];
for (int i = 1; i <= n; ++i)
scanf("%d", &b[i]);
T = 3010;
for (int i = 1; i <= n; ++i)
if (b[i] - a[i] > 0)
add_edge(S, i, b[i] - a[i]), all += b[i] - a[i];
else
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &x, &y, &z);
all += y + z;
add_edge(n + i * 2, T, y);
add_edge(S, n + i * 2 + 1, z);
for (int j = 1; j <= x; ++j)
scanf("%d", &t),
add_edge(t, n + i * 2, inf),
add_edge(n + i * 2 + 1, t, inf);
}
printf("%d\n", all - dinic(S, T));
}