1711: [Usaco2007 Open]Dining吃饭
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 916 Solved: 486
[ 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
输入解释:
牛 1: 食品从 {1,2}, 饮料从 {1,2} 中选
牛 2: 食品从 {2,3}, 饮料从 {1,2} 中选
牛 3: 食品从 {1,3}, 饮料从 {1,2} 中选
牛 4: 食品从 {1,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
输入解释:
牛 1: 食品从 {1,2}, 饮料从 {1,2} 中选
牛 2: 食品从 {2,3}, 饮料从 {1,2} 中选
牛 3: 食品从 {1,3}, 饮料从 {1,2} 中选
牛 4: 食品从 {1,3}, 饮料从 {3} 中选
Sample Output
3
输出解释:
一个方案是:
Cow 1: 不吃
Cow 2: 食品 #2, 饮料 #2
Cow 3: 食品 #1, 饮料 #1
Cow 4: 食品 #3, 饮料 #3
用鸽笼定理可以推出没有更好的解 (一共只有3总食品和饮料).当然,别的数据会更难.
输出解释:
一个方案是:
Cow 1: 不吃
Cow 2: 食品 #2, 饮料 #2
Cow 3: 食品 #1, 饮料 #1
Cow 4: 食品 #3, 饮料 #3
用鸽笼定理可以推出没有更好的解 (一共只有3总食品和饮料).当然,别的数据会更难.
HINT
Source
题解:最大流
对于每个牛拆点x,x',x->x'容量为1。源点与所有的食物相连,容量为1,所有的饮料与汇点相连,容量为1.
然后对于每头牛,他可以吃的食物向x连边,x'向他可以喝的饮料连边,容量都为1。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define N 100000
#define inf 1e9
using namespace std;
int n,f,d;
int tot,point[N],next[N],v[N],remain[N];
int deep[N],cur[N],num[N],last[N];
void add(int x,int y,int z)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
//cout<<x<<" "<<y<<" "<<tot<<endl;
tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
int addflow(int s,int t)
{
int ans=inf,now=t;
while(now!=s)
{
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while(now!=s)
{
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void bfs(int s,int t)
{
for (int i=s;i<=t;i++) deep[i]=t;
deep[t]=0;
queue<int> p; p.push(t);
while(!p.empty())
{
int now=p.front(); p.pop();
for (int i=point[now];i!=-1;i=next[i])
if (deep[v[i]]==t&&remain[i^1])
deep[v[i]]=deep[now]+1,p.push(v[i]);
}
}
int isap(int s,int t)
{
int ans=0,now=s;
for (int i=s;i<=t;i++) cur[i]=point[i];
for (int i=s;i<=t;i++) num[deep[i]]++;
while(deep[s]<t)
{
if (now==t)
{
ans+=addflow(s,t);
now=s;
}
bool f=false;
for (int i=cur[now];i!=-1;i=next[i])
if (deep[now]==deep[v[i]]+1&&remain[i])
{
last[v[i]]=i;
cur[now]=i;
now=v[i];
f=true;
break;
}
if(!f)
{
int minn=t;
for (int i=point[now];i!=-1;i=next[i])
if (remain[i]) minn=min(deep[v[i]],minn);
if (!--num[deep[now]]) break;
deep[now]=minn+1; num[deep[now]]++;
cur[now]=point[now];
if (now!=s)
now=v[last[now]^1];
}
}
return ans;
}
int main()
{
scanf("%d%d%d",&n,&f,&d);
tot=-1;
memset(point,-1,sizeof(point));
memset(next,-1,sizeof(next));
int cnt=2+f+d+n*2;
for (int i=1;i<=n;i++)
{
int x,y,m;
add(1+f+i,1+f+n+i,1);
scanf("%d%d",&x,&y);
for (int j=1;j<=x;j++)
{
scanf("%d",&m);
add(1+m,1+f+i,1);
}
for (int j=1;j<=y;j++)
{
scanf("%d",&m);
add(1+f+i+n,1+f+n*2+m,1);
}
}
for (int i=1;i<=f;i++)
add(1,1+i,1);
for (int i=1;i<=d;i++)
add(1+n*2+f+i,cnt,1);
printf("%d\n",isap(1,cnt));
}