首页 > 题解 > bzoj 3438 小M的作物

bzoj 3438 小M的作物

Description

小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物的种子,每种作物的种子有1个(就是可以种一棵作物)(用1…n编号),现在,第i种作物种植在A中种植可以获得ai的收益,在B中种植可以获得bi的收益,而且,现在还有这么一种神奇的现象,就是某些作物共同种在一块耕地中可以获得额外的收益,小M找到了规则中共有m种作物组合,第i个组合中的作物共同种在A中可以获得c1i的额外收益,共同总在B中可以获得c2i的额外收益,所以,小M很快的算出了种植的最大收益,但是他想要考考你,你能回答他这个问题么?

Input

第一行包括一个整数n
第二行包括n个整数,表示ai第三行包括n个整数,表示bi第四行包括一个整数m接下来m行,
对于接下来的第i行:第一个整数ki,表示第i个作物组合中共有ki种作物,
接下来两个整数c1i,c2i,接下来ki个整数,表示该组合中的作物编号。输出格式

Output

只有一行,包括一个整数,表示最大收益

Sample Input

3

4 2 1

2 3 2

1

2 3 2 1 2

Sample Output

11

样例解释A耕地种1,2,B耕地种3,收益4+2+3+2=11。

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

题解

首先我们先把所有的元素都放在集合A中 获得所有的a[i]和c1[i]

然后考虑最大权闭合子图

一个点如果不选就放在A集合中 选就放在B集合中

一个点如果选 那么就要扣除相应的ai并获得相应的b[i] 于是每个点的权值为b[i]-a[i]

将所有的子集拆点变成两个

一个子集中的任意一个元素选择 那么就要扣除相应的c1[i] 这个子集的权值为-c1[i] 从这个子集的所有点出发连一条到达这个点的边

一个子集中所有的元素都选择 那么就会获得相应的c2[i] 这个子集的权值为c2[i] 从这个点出发向这个子集的所有点连一条边

然后按照最大权闭合子图建图就好了

#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) {
    add(x, y, z), add(y, x, 0);
}
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
            add_edge(i, T, -(b[i] - a[i]));
    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));
}