描述
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).
输入
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.
输出
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
样例输入
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
样例输出
3
题意:农夫为他的N头牛准备了F种食物和D种饮料,每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到自己喜欢的食物和饮料?
分析:
牛要匹配食物也要匹配饮料,那就把牛放中间,跑最大流:源点->食物->牛->饮料->汇点,权值全为1。
卡数据:
1 2 2
2 2 1 2 1 2
貌似很正确,但是没有限流,也就是每头牛最多匹配一种方案,所以要把牛这个点拆成两个点,牛和牛之间也连上,起到限流的作用。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
typedef long long LL;
using namespace std;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;
int head[MAXN], dist[MAXN], vis[MAXN];
int cur[MAXN];
int top = 0;
struct Edge {
int to, cap, flow, next;
}edge[MAXN * 100];
void init() {
top = 0;
memset(head, -1, sizeof(head));
memset(vis, 0, sizeof(vis));
}
void addedge(int a, int b, int c) {
Edge E1 = {b, c, 0, head[a]};
edge[top] = E1;
head[a] = top++;
Edge E2 = {a, 0, 0, head[b]};
edge[top] = E2;
head[b] = top++;
}
bool BFS(int st, int ed) {
memset(dist, -1, sizeof(dist));
memset(vis, 0, sizeof(vis));
queue<int> que;
que.push(st);
vis[st] = 1;
dist[st] = 0;
while(!que.empty()) {
int u = que.front();
que.pop();
for(int i = head[u]; i != -1; i = edge[i].next) {
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow) {
dist[E.to] = dist[u] + 1;
vis[E.to] = 1;
if(E.to == ed) return true;
que.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int ed) {
if(x == ed || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; i != -1; i = edge[i].next) {
Edge& E = edge[i];
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), ed)) > 0) {
E.flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int st, int ed) {
int flow = 0;
while(BFS(st, ed)) {
memcpy(cur, head, sizeof(head));
flow += DFS(st, INF, ed);
}
return flow;
}
int main()
{
int N, F, D; init();
scanf("%d %d %d", &N, &F, &D);
for(int i = 1; i <= N; ++i) {
int Fi, Di, k;
scanf("%d %d", &Fi, &Di);
for(int j = 1; j <= Fi; ++j) {
scanf("%d", &k);
addedge(k, i + 100, 1);
}
addedge(i + 100, i + 200, 1);
for(int j = 1; j <= Di; ++j) {
scanf("%d", &k);
addedge(i + 200, k + 300, 1);
}
}
for(int i = 1; i <= F; ++i) addedge(0, i, 1);
for(int i = 1; i <= D; ++i) addedge(i + 300, 401, 1);
int ans = Maxflow(0, 401);
printf("%d\n", ans);
return 0;
}