Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20182 | Accepted: 8970 |
Description
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
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
Hint
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int f[500][500];//流量
int c[500][500];//容量
int a[500],pre[500];//a是标记加保存最小的容量与流量的差值
int ek(int s,int t)
{
int sum=0;
queue<int>q;
while(1)
{
memset(a,0,sizeof(a));
a[s]=inf;
q.push(s);
while(!q.empty())//找增广路
{
int u=q.front();
q.pop();
for(int v=s;v<=t;v++)
{
if(!a[v]&&c[u][v]>f[u][v])
{
a[v]=min(a[u],c[u][v]-f[u][v]);//上一节点能流的流量和路径未被使用的部分比较
pre[v]=u;
q.push(v);
}
}
}
if(!a[t]) break;//找不到就跳出
for(int i=t;i!=s;i=pre[i])//调整流量
{
f[pre[i]][i]+=a[t];
f[i][pre[i]]-=a[t];//为了让所有的残余流量都能用上
}
sum+=a[t];
}
return sum;
}
int main()
{
int n,ff,d,a,b,xf,xd;
while(~scanf("%d%d%d",&n,&ff,&d))
{
memset(c,0,sizeof(c));
memset(f,0,sizeof(f));
for(int i=1;i<=ff;i++)
c[0][i]=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
while(a--)
{
scanf("%d",&xf);
c[xf][i+ff]=1;
}
c[i+ff][i+ff+n]=1;
while(b--)
{
scanf("%d",&xd);
c[i+ff+n][xd+ff+2*n]=1;
}
}
for(int i=1;i<=d;i++)
c[ff+2*n+i][ff+2*n+d+1]=1;
printf("%d\n",ek(0,ff+2*n+d+1));
}
}