1711: [Usaco2007 Open]Dingin吃饭
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 710 Solved: 365
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} 中选
Sample Output
3
输出解释:
一个方案是:
Cow 1: 不吃
Cow 2: 食品 #2, 饮料 #2
Cow 3: 食品 #1, 饮料 #1
Cow 4: 食品 #3, 饮料 #3
用鸽笼定理可以推出没有更好的解 (一共只有3总食品和饮料).当然,别的数据会更难.
Source
Gold
传说中的三分图匹配么。。。233
把每头牛拆成x和x’,然后连一条流量为1的边。。
每种食物向源点连边,每种饮料向汇点连边,流量都为1
然后每头牛和食物、饮料连边,流量为1
最大流即可。。
4A。。我太弱。。
附上本蒟蒻的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define T 501
int n,f,d,cnt=1,head,tail,ans,sum,h[100001],dis[100001],q[100001];
struct kx
{
int to,next,v;
}edge[100001];
int read()
{
int w=0,c=1;
char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-') c=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
w=w*10+ch-'0',ch=getchar();
return w*c;
}
void add(int u,int v,int w)
{
cnt++,edge[cnt].next=h[u],h[u]=cnt,edge[cnt].to=v,edge[cnt].v=w;
cnt++,edge[cnt].next=h[v],h[v]=cnt,edge[cnt].to=u,edge[cnt].v=0;
}
bool bfs()
{
int j,p;
memset(dis,-1,sizeof(dis));
q[0]=0,dis[0]=0;
head=0,tail=1;
while (head<tail)
{
head++,j=q[head],p=h[j];
while (p)
{
if (dis[edge[p].to]<0 && edge[p].v>0)
dis[edge[p].to]=dis[j]+1,tail++,q[tail]=edge[p].to;
p=edge[p].next;
}
}
if (dis[T]>0)
return true;
else
return false;
}
int dfs(int x,int f)
{
int w,used=0,i=h[x];
if (x==T)
return f;
while (i)
{
if (edge[i].v && dis[edge[i].to]==dis[x]+1)
{
w=f-used,w=dfs(edge[i].to,min(w,edge[i].v));
edge[i].v-=w,edge[i^1].v+=w,used+=w;
if (used==f) return f;
}
i=edge[i].next;
}
if (!used) dis[x]=-1;
return used;
}
int main()
{
int i,j,x,y;
n=read(),f=read(),d=read();
for (i=1;i<=n;i++)
add(i,i+n,1);
for (i=1;i<=f;i++)
add(0,i+2*n,1);
for (i=1;i<=d;i++)
add(i+2*n+f,T,1);
for (i=1;i<=n;i++)
{
y=read(),d=read();
for (j=1;j<=y;j++)
x=read(),add(x+2*n,i,1);
for (j=1;j<=d;j++)
x=read(),add(n+i,x+2*n+f,1);
}
ans=0;
while (bfs())
while (sum=dfs(0,0x7fffffff))
ans+=sum;
printf("%d",ans);
return 0;
}