luogu1231 教辅的组成

题目

  https://www.luogu.org/problem/show?pid=1231

题解

  上面是练习册,中间是书(拆点限流),下面是答案。做最大流。

代码

//最大流
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 300000
#define inf 0x3f3f3f3f
using namespace std;=
int head[maxn], next[maxn], to[maxn], c[maxn], d[maxn], num[maxn], Exit, N1, N2, N3, M1,
	M2, S, T, tot=1, last[maxn];
void adde(int a, int b, int cc){to[++tot]=b;c[tot]=cc;next[tot]=head[a];head[a]=tot;}
void adde2(int a, int b, int cc){adde(a,b,cc),adde(b,a,0);}
int isap(int pos, int in)
{
	int flow=0, p, t;
	if(pos==T)return in;
	for(p=last[pos];p;last[pos]=p=next[p])
		if(c[p] and d[to[p]]+1==d[pos])
		{
			flow+= t=isap(to[p],min(c[p],in-flow));
			c[p]-=t, c[p xor 1]+=t;
			if(Exit or in==flow)return flow;
		}
	Exit=--num[d[pos]]==0;
	++num[++d[pos]];
	last[pos]=head[pos];
	return flow;
}
void build()
{
	int i, x, y;
	scanf("%d%d%d",&N1,&N2,&N3);
	S=N2+N1*2+N3+10;T=S+1;
	scanf("%d",&M1);
	for(i=1;i<=M1;i++)scanf("%d%d",&x,&y),adde2(y,N2+x,1);
	scanf("%d",&M2);
	for(i=1;i<=M2;i++)scanf("%d%d",&x,&y),adde2(N2+x+N1,N2+N1*2+y,1);
	for(i=1;i<=N2;i++)adde2(S,i,1);
	for(i=1;i<=N1;i++)adde2(N2+i,N2+i+N1,1);
	for(i=1;i<=N3;i++)adde2(N2+N1*2+i,T,1);
}
int main()
{
	int ans=0;
	build();
	while(!Exit)ans+=isap(S,inf);
	printf("%d",ans);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值