[bzoj3438]小M的作物【最小割】

【题目链接】
  http://www.lydsy.com/JudgeOnline/problem.php?id=3438
【题解】
  一个不算太复杂的最小割模型。
  首先 S S 往每个点连边,流量为a[i],每个点往 T T 连边,容量为b[i]
  对于每个组合,新建两个点 u,v u , v S S u连边,流量为 c1 c 1 u u 向每个涉及到的点连边,流量为inf v v T连边,流量为 c2 c 2 ,每个涉及的点往 v v 连边,流量为inf
  所有权值相加减去最大流即为答案。
  说明一下组合的情况,若所有相关的点与 T T 的边都被割掉了,那么c1就能取, c2 c 2 同理。
  

/* --------------
    user Vanisher
    problem bzoj-3438
----------------*/
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    N       10010
# define    M       5000100
using namespace std;
int read(){
    int tmp=0, fh=1; char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') fh=-1; ch=getchar();}
    while (ch>='0'&&ch<='9'){tmp=tmp*10+ch-'0'; ch=getchar();}
    return tmp*fh;
}

int a[N],b[N],ans,m;

struct node{
    int data,next,vote,re,l;
}e[M];
int dist[N],q[N],place,head[N],now[N],n,S,T,id;
void build(int u, int v, int l){
    e[++place].data=v; e[place].next=head[u]; head[u]=place; e[place].l=l; e[place].re=place+1;
    e[++place].data=u; e[place].next=head[v]; head[v]=place; e[place].l=0; e[place].re=place-1;
}
void bfs(int S, int T){
    memset(dist,inf,sizeof(dist));
    dist[S]=0; int pl=1,pr=1; q[1]=0;
    while (pl<=pr){
        int x=q[pl++];
        for (int start=head[x]; start!=0; start=e[start].next)
            if (e[start].l>0&&dist[e[start].data]==inf){
                dist[e[start].data]=dist[x]+1;
                q[++pr]=e[start].data;
            }
    } 
}
int dfs(int x, int T, int flow){
    if (x==T) return flow; int sum=0;
    for (int start=now[x]; start!=0; start=e[start].next){
        if (e[start].l>0&&dist[e[start].data]==dist[x]+1){
            int l=dfs(e[start].data,T,min(e[start].l,flow));
            sum+=l; flow-=l;
            e[start].l-=l; e[e[start].re].l+=l;
            if (flow==0){
                now[x]=start;
                return sum;
            }
        }
    }
    now[x]=0; return sum;
}
int dinic(int S, int T){
    int sum=0;
    for (bfs(S,T); dist[T]!=inf; bfs(S,T)){
        memcpy(now,head,sizeof(now));
        sum+=dfs(S,T,inf);
    }
    return sum;
} 

int main(){
    n=read();
    S=0, T=n+1, id=n+1;
    for (int i=1; i<=n; i++){
        a[i]=read(), ans=ans+a[i];
        build(S,i,a[i]);
    }
    for (int i=1; i<=n; i++){
        b[i]=read(), ans=ans+b[i];
        build(i,T,b[i]);
    }
    m=read();
    for (int i=1; i<=m; i++){
        int nowl=++id, nowr=++id;
        int num=read(), wl=read(), wr=read();
        build(S,nowl,wl);
        build(nowr,T,wr);
        ans=ans+wl+wr;
        for (int j=1; j<=num; j++){
            int x=read();
            build(nowl,x,inf);
            build(x,nowr,inf);
        }
    }
    ans=ans-dinic(S,T);
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值