Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10184 | Accepted: 4682 |
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 Fiintegers 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
Dinic算法,每次先用bfs构造层次网络,dfs按层次增广下去,直到无法在增广下去(即汇点t的最短距离改变了)重新bfs构造层次网络。算法直到s-t没有路径为止。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
#define maxn 405
#define maxm maxn*maxn
int n,f,d;
struct edge
{
edge(int a, int b, int c){ to = a; cap = b; rev = c;}
int to, cap, rev;
};
vector<edge> g[maxn];
void add(int u, int v)
{
g[u].push_back(edge(v, 1, g[v].size()));
g[v].push_back(edge(u,0, g[u].size()-1));
}
int cur[maxn];
int level[maxn];
void bfs(int s)
{
memset(level, -1, sizeof(level));
level[s] = 0;
queue<int> que;
que.push(s);
while(!que.empty()){
int u = que.front(); que.pop();
for(int i = 0; i < g[u].size(); i++){
int v = g[u][i].to, cap = g[u][i].cap;
if(level[v] == -1 && cap > 0){
level[v] = level[u]+1;
que.push(v);
}
}
}
}
int dfs(int u, int t, int f)
{
if(u == t || f == 0) return f;
for(int &i = cur[u]; i < g[u].size(); i++){
int v = g[u][i].to, &cap = g[u][i].cap, rev = g[u][i].rev;
int d = 0;
if(level[v] == level[u]+1 && cap > 0){
d = dfs(v, t,min(cap, f));
if(d > 0){
cap -= d;
g[v][rev].cap += d;
return d;
}
}
}
return 0;
}
int dinic(int s, int t)
{
int max_flow = 0;
while(1){
bfs(s);
if(level[t] < 0) return max_flow;
memset(cur, 0, sizeof(cur));
int f;
while((f = dfs(s, t, 0x3f3f3f3f))) max_flow += f;
}
}
int main()
{
while(~scanf("%d %d %d", &n, &f, &d)){
for(int i = 0; i < maxn; i++) g[i].clear();
int s = 0, t = 2*n+f+d+1;
int v,fc, dc;
for(int i = 1; i <= n; i++){
add(f+i,f+n+i);
scanf("%d%d", &fc, &dc);
for(int j = 0; j < fc; j++){
scanf("%d", &v);
add(v, f+i);
}
for(int j= 0; j < dc; j++){
scanf("%d", &v);
v += f+2*n;
add(f+n+i, v);
}
}
for(int i = 1; i <= f; i++)
add(s, i);
for(int i = f+2*n+1; i <= f+2*n+d; i++)
add(i, t);
printf("%d\n", dinic(s,t));
}
return 0;
}