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.
//
有n头牛,F种食物,D种饮料,
第i头牛喜欢fi种食物,di种饮料,编号分别为。。。
已知一头牛最多能吃一种食物和一种饮料,每种饮料
或食物最多能被一头牛吃,求以上条件下,最多能有多少头
牛能吃到他所喜爱的食物和饮料
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1410;
const int M=50000;
const int inf=(1<<28);
int head[N];
struct Edge
{
int v,next,w;
int pw;//原图中u->v的边权
} edge[M];
int cnt,n,s,t;//n从0开始 0->n-1
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].pw=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].pw=w;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int sap()
{
int pre[N],cur[N],dis[N],gap[N];
int flow=0,aug=inf,u;
bool flag;
for(int i=0; i<n; i++)
{
cur[i]=head[i];
gap[i]=dis[i]=0;
}
gap[s]=n;
u=pre[s]=s;
while(dis[s]<n)
{
flag=0;
for(int &j=cur[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].w<aug) aug=edge[j].w;
pre[v]=u;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
edge[cur[u]].w-=aug;
edge[cur[u]^1].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(int j=head[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)
break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
//初始化 cnt=0;memset(head,-1,sizeof(head));
//把一头牛拆成两个点,之间连边。S向所有食物连边,所有食物向喜欢它的牛左边的点
//连边,所有牛右边的点向它喜欢的饮料连边,所有饮料向T连边。权值统统为1。然后
//做一遍最大流
int main()
{
int m,f,d;
while(scanf("%d%d%d",&m,&f,&d)==3)
{
cnt=0;
memset(head,-1,sizeof(head));
n=2*m+f+d+2;
s=0,t=n-1;
//牛拆点 s->food->niu1->niur->drink->t
for(int i=1;i<=f;i++)
{
addedge(0,2*m+i,1);
}
for(int i=1;i<=d;i++)
{
addedge(2*m+f+i,t,1);
}
for(int i=1;i<=m;i++)
{
addedge(i,i+m,1);
int lf,ld;scanf("%d%d",&lf,&ld);
for(int j=1;j<=lf;j++)
{
int x;scanf("%d",&x);
addedge(2*m+x,i,1);
}
for(int j=1;j<=ld;j++)
{
int x;scanf("%d",&x);
addedge(i+m,2*m+f+x,1);
}
}
int ans=sap();
printf("%d\n",ans);
}
return 0;
}
358

被折叠的 条评论
为什么被折叠?



