Dining
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
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. Source |
题意:
多重的匹配,每只牛喜欢一种的food和drink,问最多几只牛。
POINT:
s-food-牛1-牛2-drink-t。
牛1牛2其实是同一只牛。
4 3 3 2 2 1 2 1 3 0 0 0 0 0 0参考这一组测试样例。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
#define LL long long
const int inf = 0x3f3f3f3f;
const int maxn = 600;
struct edge
{
int from,to,flow,cap;
edge(int u,int v,int f,int c):
from(u),to(v),flow(f),cap(c){};
};
vector<int>G[maxn];
vector<edge>len;
void add(int u,int v,int c)
{
len.push_back(edge(u,v,0,c));
len.push_back(edge(v,u,0,0));
G[u].push_back(len.size()-2);
G[v].push_back(len.size()-1);
}
int d[maxn],s,t;
bool bfs()
{
int vis[maxn];
memset(vis,0,sizeof vis);
vis[s]=1;
queue<int>q;
d[s]=1;
q.push(s);
while(!q.empty())
{
int u = q.front();q.pop();
for(int i=0;i<G[u].size();i++)
{
edge e = len[G[u][i]];
if(e.cap>e.flow&&!vis[e.to])
{
d[e.to]=d[e.from]+1;
vis[e.to]=1;
q.push(e.to);
}
}
}
return vis[t];
}
int cur[maxn];
int dfs(int u,int a)
{
if(u==t||a==0) return a;
int flow=0,f;
for(int &i=cur[u];i<G[u].size();i++)
{
edge &e = len[G[u][i]];
if(d[e.to]==d[e.from]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
len[G[u][i]^1].flow-=f;
flow+=f;
a-=f;
}
if(a==0) break;
}
if(flow==0) d[u]=-1;
return flow;
}
int maxflow()
{
int ans=0;
while(bfs())
{
memset(cur,0,sizeof cur);
ans+=dfs(s,inf);
}
return ans;
}
int main()
{
int n,f,d;
scanf("%d %d %d",&n,&f,&d);
s=0,t=f+d+n+n+1;
for(int i=1;i<=n;i++)
{
int fi,di;scanf("%d %d",&fi,&di);
int ff[maxn],dd[maxn];
for(int j=1;j<=fi;j++)
{
scanf("%d",&ff[j]);
add(ff[j],f+i,1);
}
for(int j=1;j<=di;j++)
{
scanf("%d",&dd[j]);
add(i+n+f,n+n+f+dd[j],1);
}
}
for(int i=1;i<=n;i++)
{
add(f+i,f+i+n,1);
}
for(int i=1;i<=f;i++)
{
add(0,i,1);
}
for(int i=1;i<=d;i++)
{
add(n+n+f+i,t,1);
}
printf("%d\n",maxflow());
}