[bzoj3438]小M的作物【最小割】
【题目链接】
http://www.lydsy.com/JudgeOnline/problem.php?id=3438
【题解】
一个不算太复杂的最小割模型。
首先往每个点连边,流量为,每个点往连边,容量为。
对于每个组合,新建两个点,向连边,流量为,向每个涉及到的点连边,流量为。向连边,流量为,每个涉及的点往连边,流量为。
所有权值相加减去最大流即为答案。
说明一下组合的情况,若所有相关的点与的边都被割掉了,那么就能取,同理。
/* --------------
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;
}