首页 > 代码库 > POJ 3281

POJ 3281

Dining
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8577   Accepted: 3991

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: NF, 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

USACO 2007 Open Gold
 
最大流
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <vector>
  6 #include <queue>
  7 
  8 using namespace std;
  9 
 10 const int MAX_N = 500;
 11 struct Edge {int from,to,cap,flow; };
 12 int N,F,D;
 13 vector<Edge> edges;
 14 vector<int> G[MAX_N];
 15 int d[MAX_N],cur[MAX_N];
 16 bool vis[MAX_N];
 17 
 18 void add_edge(int from,int to,int cap) {
 19         edges.push_back(Edge {from, to, cap, 0});
 20         edges.push_back(Edge {to, from, 0, 0});
 21         int m = edges.size();
 22         G[from].push_back(m - 2);
 23         G[to].push_back(m - 1);
 24 }
 25 
 26 bool bfs(int s,int t) {
 27         memset(vis,0,sizeof(vis));
 28         queue <int> q;
 29         q.push(s);
 30         d[s] = 0;
 31         vis[s] = 1;
 32         while(!q.empty()) {
 33                 int x = q.front(); q.pop();
 34                 for(int i = 0; i < G[x].size(); ++i) {
 35                         Edge &e = edges[ G[x][i] ];
 36                         if(!vis[e.to] && e.cap > e.flow) {
 37                                 vis[e.to] = 1;
 38                                 d[e.to] = d[x] + 1;
 39                                 q.push(e.to);
 40                         }
 41                 }
 42         }
 43 
 44         return vis[t];
 45 }
 46 
 47 int dfs(int x,int a,int t) {
 48         if(x == t || a == 0)  return a;
 49         int flow = 0, f;
 50         for(int& i = cur[x]; i < G[x].size(); ++i) {
 51                 Edge &e = edges[ G[x][i] ];
 52                 if(d[x] + 1 == d[e.to] && (f = dfs(e.to,min(a,e.cap - e.flow),t)) > 0 ) {
 53                         e.flow += f;
 54                         edges[ G[x][i] ^ 1 ].flow -= f;
 55                         flow += f;
 56                         a -= f;
 57                         if(a == 0) break;
 58                 }
 59         }
 60 
 61         return flow;
 62 }
 63 
 64 int Maxflow(int s,int t) {
 65         int flow = 0;
 66         while(bfs(s,t)) {
 67                 //printf("fucl\n");
 68                 memset(cur, 0, sizeof(cur));
 69                 flow += dfs(s, 1, t);
 70         }
 71 
 72         return flow;
 73 }
 74 int main()
 75 {
 76    // freopen("sw.in","r",stdin);
 77     scanf("%d%d%d",&N,&F,&D);
 78     for(int i = 1; i <= N; ++i) {
 79             add_edge(i,i + N,1);
 80     }
 81     for(int i = 1; i <= N; ++i) {
 82             int f,d;
 83             scanf("%d%d",&f,&d);
 84             for(int j = 1; j <= f; ++j) {
 85                     int ch;
 86                     scanf("%d",&ch);
 87                     add_edge(2 * N + ch,i,1);
 88             }
 89             for(int j = 1; j <= d; ++j) {
 90                     int ch;
 91                     scanf("%d",&ch);
 92                     add_edge(i + N,ch + 2 * N + F,1);
 93             }
 94 
 95     }
 96 
 97     for(int i = 1; i <= F; ++i) {
 98             add_edge(0,i + 2 * N,1);
 99     }
100     for(int i = 1; i <= D; ++i) {
101             add_edge(i + 2 * N + F,2 * N + F + D + 1,1);
102     }
103 
104     printf("%d\n",Maxflow(0,2 * N + F + D + 1));
105 
106    // cout << "Hello world!" << endl;
107     return 0;
108 }
View Code