Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17845 | Accepted: 7960 |
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.
Source
题意:有N头牛,F种食物,D种饮料,每只牛都有喜爱的食物和饮料,且每只牛只能选择一种食物和饮料,最优分配方案下,问最多能使多少只牛满足食物和饮料都是它喜爱的。
思路:最大流,超级源点指向食物,食物指向喜爱它的牛,牛指向牛(指向自己的副本,保证每只牛只能选取一份食物和饮料),副本牛指向喜爱的饮料,饮料指向汇点,所有边权均为1,求最大流即可,下面用Dinic,0为超级源点,i为牛,i+100为食物,i+200为牛副本,i+300为饮料,488为汇点。
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
const int maxn = 3e4;
const int N = 500;
const int INF = 0x7fffffff;
int cnt=0, Next[N], q[N], dis[N];
struct node
{
int e, next, w;
}edge[maxn];
void add_edge(int u, int v, int w)
{
edge[cnt].e = v;
edge[cnt].w = w;
edge[cnt].next = Next[u];
Next[u] = cnt++;
edge[cnt].e = u;
edge[cnt].w = 0;
edge[cnt].next = Next[v];
Next[v] = cnt++;
}
bool bfs()
{
memset(dis, -1, sizeof(dis));
dis[0] = 0;
int l=0, r=0;
q[r++] = 0;
while(l<r)
{
int u = q[l];
++l;
for(int i=Next[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].e, w = edge[i].w;
if(dis[v] == -1 && w > 0)
{
dis[v] = dis[u] + 1;
q[r++] = v;
}
}
}
return dis[488] != -1;
}
int dfs(int u, int pre)
{
if(u == 488) return pre;
int ans = 0, f = 0;
for(int i=Next[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].e, w = edge[i].w;
if(dis[v]==dis[u]+1 && w>0 && (f=dfs(v, min(pre, w))))
{
edge[i].w -= f;
edge[i^1].w += f;
ans += f;
pre -= f;
if(!pre)
break;
}
}
if(ans)
return ans;
dis[u] = -1;
return 0;
}
int main()
{
int n, f, d, a, b, fo, dr;
while(~scanf("%d%d%d",&n,&f,&d))
{
cnt = 0;
memset(Next, -1, sizeof(Next));
for(int i=1; i<=n; ++i)
{
scanf("%d%d",&a,&b);
while(a--)
scanf("%d",&fo), add_edge(fo+100, i, 1);
add_edge(i, i+200, 1);
while(b--)
scanf("%d",&dr), add_edge(i+200, dr+300, 1);
}
for(int i=1; i<=f; ++i) add_edge(0, i+100, 1);
for(int i=1; i<=d; ++i) add_edge(i+300, 488, 1);
int max_flow = 0;
while(bfs()) max_flow += dfs(0, INF);
printf("%d\n",max_flow);
}
return 0;
}