题目链接
http://poj.org/problem?id=3281
题目
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
Line 1: Three space-separated integers: N, F, and D
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
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
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
One way to satisfy three cows is:
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 头(1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?
题解:
顾客同时需要满足饮料和食物两种需求,可以引一条从食物到顾客的有向弧、一条顾客到饮料的有向弧。那么每一条可行流都对应一个被满足的顾客,但不是一一对应。要满足一一对应关系,即顾客结点的结点容量为1,需要将顾客进行拆点。顾客u拆成u1和u2,u1、u2之间连一条容量为u结点容量的有向弧。原指向u的边改成指向u1,原从u指出的边改成从u2指出。
如此,将饮料看作源点、食物看作汇点,这就是一个多源多汇模型。每条流量对应一个被满足的顾客。
解决多源多汇模型,设立一个超级源点S,引入从S到源点的有向弧,容量为1.设立一个超级汇点T,引入汇点到超级汇点的有向弧,容量为1.
超级源点到超级汇点的最大流即为答案。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
int N,F,D;
int S,T;//源点和汇点
const int maxe=1e4+1000;
const int maxv=1100;
struct edge
{
int to,w,next;
}e[maxe<<1];
int head[maxv<<1],depth[maxv],cnt;
void init()
{
memset(head,-1,sizeof(head));
cnt=-1;
}
void add_edge(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
void _add(int u,int v,int w)
{
add_edge(u,v,w);
add_edge(v,u,0);
}
bool bfs()
{
queue<int> q;
while(!q.empty()) q.pop();
memset(depth,0,sizeof(depth));
depth[S]=1;
q.push(S);
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
if(!depth[e[i].to] && e[i].w>0)
{
depth[e[i].to]=depth[u]+1;
q.push(e[i].to);
}
}
}
if(!depth[T]) return false;
return true;
}
int dfs(int u,int flow)
{
if(u==T) return flow;
int ret=0;
for(int i=head[u];i!=-1 && flow;i=e[i].next)
{
if(depth[u]+1==depth[e[i].to] && e[i].w!=0)
{
int tmp=dfs(e[i].to,min(e[i].w,flow));
if(tmp>0)
{
flow-=tmp;
ret+=tmp;
e[i].w-=tmp;
e[i^1].w+=tmp;
}
}
}
return ret;
}
void Dinic()
{
int ans=0;
while(bfs())
{
ans+=dfs(0,INF);
}
printf("%d\n",ans);
}
int main()
{
init();
S=0,T=600;
scanf("%d%d%d",&N,&F,&D);
for(int i=1;i<=F;i++) _add(S,i,1);//源点到食物
for(int i=1;i<=D;i++) _add(100+i,T,1);//饮料到汇点
for(int i=1;i<=N;i++)//i+200和i+300表示牛的编号
{
int f,d;
scanf("%d%d",&f,&d);
_add(i+200,i+300,1);//牛到牛
while(f--)//食物
{
int u;
scanf("%d",&u);
_add(u,i+200,1);//食物到牛
}
while(d--)//饮料
{
int u;
scanf("%d",&u);
_add(i+300,u+100,1);
}
}
Dinic();
return 0;
}