3438: 小M的作物

3438: 小M的作物

Time Limit: 10 Sec   Memory Limit: 256 MB
Submit: 1078   Solved: 468
[ Submit][ Status][ Discuss]

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。

HINT

Source

[ Submit][ Status][ Discuss]

考虑最大收益 = 总收益 - 最小割,将此题转换成最小割模型
这是一种多元限制关系,对于每种作物i,从s向i连容量为ai的边,i向t连容量为bi的边
若作物i最后在s割,说明种在A地,否则在B地,考虑限制关系
对于第i个限制关系,拆成两个点ui,vi,从s向ui连容量为c1i的边,ui向集合内每个点连容量为INF的边
同理,vi向t连容量为c2i的边,集合内每个点向vi连容量为INF的边,此图最大流就是最小割了
证明:容量INF的边保证限制点要么和所有作物在同一个割集,要么支付这个代价
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
 
const int maxn = 3030;
const int maxm = 4E6;
const int INF = ~0U>>1;
 
struct E{
    int to,cap,flow; E(){}
    E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];
 
int n,m,s,t,cnt,Ans,tot,A[maxn],B[maxn],L[maxn],cur[maxn];
 
vector <int> v[maxn];
queue <int> Q;
 
void Add(int x,int y,int cap)
{
    v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0);
    v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}
 
bool BFS()
{
    for (int i = s; i <= t; i++) L[i] = 0;
    L[s] = 1; Q.push(s);
    while (!Q.empty())
    {
        int k = Q.front(); Q.pop();
        for (int i = 0; i < v[k].size(); i++)
        {
            E e = edgs[v[k][i]];
            if (e.cap == e.flow || L[e.to]) continue;
            L[e.to] = L[k] + 1; Q.push(e.to);
        }
    }
    return L[t];
}
 
int Dinic(int x,int a)
{
    if (x == t) return a; int flow = 0;
    for (int &i = cur[x]; i < v[x].size(); i++)
    {
        E &e = edgs[v[x][i]];
        if (e.cap == e.flow || L[e.to] != L[x] + 1) continue;
        int f = Dinic(e.to,min(a,e.cap - e.flow));
        if (!f) continue; e.flow += f; flow += f;
        edgs[v[x][i]^1].flow -= f; a -= f;
        if (!a) return flow;
    }
    if (!flow) L[x] = -1; return flow;
}
 
int getint()
{
    char ch = getchar(); int ret = 0;
    while (ch < '0' || '9' < ch) ch = getchar();
    while ('0' <= ch && ch <= '9')
        ret = ret*10 + ch - '0',ch = getchar();
    return ret;
}
 
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif
     
    n = tot = getint();
    for (int i = 1; i <= n; i++) A[i] = getint(),Ans += A[i];
    for (int i = 1; i <= n; i++) B[i] = getint(),Ans += B[i];
    m = getint(); t = n + 2 * m + 1;
    for (int i = 1; i <= m; i++)
    {
        int k = getint(),x,a = ++tot,b = ++tot;
        x = getint(); Ans += x; Add(s,a,x);
        x = getint(); Ans += x; Add(b,t,x);
        while (k--) x = getint(),Add(a,x,INF),Add(x,b,INF);
    }
    for (int i = 1; i <= n; i++) Add(s,i,A[i]),Add(i,t,B[i]);
     
    int MaxFlow = 0;
    while (BFS())
    {
        for (int i = s; i <= t; i++) cur[i] = 0;
        MaxFlow += Dinic(s,INF); //cout << MaxFlow << endl;
    }
    cout << Ans - MaxFlow << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值