题意:有N只牛,F种食物和D种饮料,每只牛最多只能吃它们喜爱的一种食物和一种饮料,问最多能满足多少只牛,所以建图时,如果是源点(0)——食物——牛——饮料——汇点(t),权值全为1,这样会导致多条流从食物——牛——饮料,这样就不符合题意的每只牛最多只能吃它们喜爱的一种食物和一种饮料,所以当建图是源点(0)——食物——牛——牛——饮料——汇点(t)时,就只能有一条路从食物——牛——牛——饮料,建如下图,然后用最简单的KM网络流算法就行了,模板紫书有,建点的标记为,源点为0,食物为1到F,牛为F+1到F+2N,饮料为F+2N+1到F+2N+D,汇点为t。
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 <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#define maxn 405
#define INF 999999999
using namespace std;
struct Edge
{
int from, to, cap, flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
int N, F, D;
vector<Edge> edges;
vector<int> G[maxn];
int a[maxn];
int p[maxn];
void init()
{
for (int i = 0; i < 405; i++)
edges.clear();
memset(p, 0, sizeof(p));
}
void AddEdge(int from, int to, int cap)
{
int mm;
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
mm = edges.size();
G[from].push_back(mm - 2);
G[to].push_back(mm - 1);
}
int Maxflow(int s, int t)
{
int flow = 0;
while (true)
{
memset(a, 0, sizeof(a));
queue<int> q;
q.push(s);
a[s] = INF;
while (!q.empty())
{
int x = q.front(); q.pop();
for (int i = 0; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if (!a[e.to] && e.cap > e.flow)
{
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap - e.flow);
q.push(e.to);
}
}
if (a[t])
break;
}
if (!a[t]) break;
for (int u = t; u != s; u = edges[p[u]].from)
{
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
flow += a[t];
}
return flow;
}
int main()
{
while (scanf("%d%d%d", &N, &F, &D) != EOF)
{
init();
int s = 0, t = F + 2 * N + D + 1;
int ff, dd, aa, bb;
for (int i = 1; i <= F; i++)
AddEdge(0, i, 1);
for (int i = 1; i <= N; i++)
{
scanf("%d%d", &ff, &dd);
for (int j = 0; j < ff; j++)
{
scanf("%d", &aa);
AddEdge(aa, F + i, 1);
}
for (int j = 0; j < dd; j++)
{
scanf("%d", &aa);
AddEdge(F + N + i, F + 2 * N + aa, 1);
}
}
for (int i = 1; i <= D; i++)
AddEdge(F + 2 * N + i, t, 1);
for (int i = 1; i <= N; i++)
AddEdge(F + i, F + N + i, 1);
printf("%d\n",Maxflow(s, t));
}
}