bzoj 1711: [Usaco2007 Open]Dining吃饭(最大流)

1711: [Usaco2007 Open]Dining吃饭

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 1107   Solved: 600
[ Submit][ Status][ Discuss]

Description

农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. 农夫JOHN做了F (1 <= F <= 100) 种食品并准备了D (1 <= D <= 100) 种饮料. 他的N (1 <= N <= 100)头牛都以决定了是否愿意吃某种食物和喝某种饮料. 农夫JOHN想给每一头牛一种食品和一种饮料,使得尽可能多的牛得到喜欢的食物和饮料. 每一件食物和饮料只能由一头牛来用. 例如如果食物2被一头牛吃掉了,没有别的牛能吃食物2.

Input

* 第一行: 三个数: N, F, 和 D

* 第2..N+1行: 每一行由两个数开始F_i 和 D_i, 分别是第i 头牛可以吃的食品数和可以喝的饮料数.下F_i个整数是第i头牛可以吃的食品号,再下面的D_i个整数是第i头牛可以喝的饮料号码.

Output

* 第一行: 一个整数,最多可以喂饱的牛数.

Sample Input

4 3 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3

Sample Output

3


下面所有边的容量全为1:

①源点与所有食物连一条边

②所有饮料和汇点连一条边

③将每头牛拆成两个点,一个和食物连边(注意方向),一个和饮料连边

求出最大流OK

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int n, m, cnt, S, T, head[2005], h[2005], cur[2005];
typedef struct
{
	int to, next;
	int flow;
}Road;
Road G[20005];
void Add(int u, int v, int flow)
{
	cnt++;
	G[cnt].next = head[u];
	head[u] = cnt;
	G[cnt].to = v;
	G[cnt].flow = flow;
}
int Jud()
{
	int now, i;
	queue<int> q;
	memset(h, -1, sizeof(h));
	q.push(S);
	h[S] = 0;
	while(q.empty()==0)
	{
		now = q.front();
		q.pop();
		for(i=head[now];i!=0;i=G[i].next)
		{
			if(G[i].flow && h[G[i].to]==-1)
			{
				h[G[i].to] = h[now]+1;
				q.push(G[i].to);
			}
		}
	}
	if(h[T]!=-1)
		return 1;
	return 0;
}
int Sech(int x, int flow)
{
	int w, used, i;
	if(x==T)
		return flow;
	used = 0;
	for(i=cur[x];i!=0;i=G[i].next)
	{
		if(h[G[i].to]==h[x]+1)
		{
			w = Sech(G[i].to, min(flow-used, G[i].flow));
			G[i].flow -= w;
			G[i^1].flow += w;
			if(G[i].flow)
				cur[x] = i;
			used += w;
			if(used==flow)
				return flow;
		}
	}
	if(used==0)
		h[x] = -1;
	return used;
}
int Dinic()
{
	int i, flow = 0;
	while(Jud())
	{
		for(i=S;i<=T;i++)
			cur[i] = head[i];
		flow += Sech(S, 1<<25);
	}
	return flow;
}
int main(void)
{
	int n, a, b, i, x, y, t;
	scanf("%d%d%d", &n, &a, &b);
	cnt = 1, S = 0, T = a+b+n*2+1;
	for(i=1;i<=a;i++)
	{
		Add(S, i, 1);
		Add(i, S, 0);
	}
	for(i=1;i<=b;i++)
	{
		Add(a+2*n+i, T, 1);
		Add(T, a+2*n+i, 0);
	}
	for(i=1;i<=n;i++)
	{
		Add(a+i, a+n+i, 1);
		Add(a+n+i, a+i, 0);
	}
	for(i=1;i<=n;i++)
	{
		scanf("%d%d", &x, &y);
		while(x--)
		{
			scanf("%d", &t);
			Add(t, a+i, 1);
			Add(a+i, t, 0);
		}
		while(y--)
		{
			scanf("%d", &t);
			Add(a+n+i, a+2*n+t, 1);
			Add(a+2*n+t, a+n+i, 0);
		}
	}
	printf("%d\n", Dinic());
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值