您的位置:首页 > 其它

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

2018-03-16 20:33 295 查看
【题目链接】

  http://www.lydsy.com/JudgeOnline/problem.php?id=3438

【题解】

  一个不算太复杂的最小割模型。

  首先SS往每个点连边,流量为a[i]a[i],每个点往TT连边,容量为b[i]b[i]。

  对于每个组合,新建两个点u,vu,v,SS向
4000
uu连边,流量为c1c1,uu向每个涉及到的点连边,流量为infinf。vv向TT连边,流量为c2c2,每个涉及的点往vv连边,流量为infinf。

  所有权值相加减去最大流即为答案。

  说明一下组合的情况,若所有相关的点与TT的边都被割掉了,那么c1c1就能取,c2c2同理。

  

/* --------------
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
,b
,ans,m;

struct node{
int data,next,vote,re,l;
}e[M];
int dist
,q
,place,head
,now
,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: