POJ3281(KB11-B 最大流)

Dining

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19170   Accepted: 8554

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

 
题意:有N头牛,F个食物,D个饮料。
N头牛每头牛有一定的喜好,只喜欢几个食物和饮料。
每个食物和饮料只能给一头牛。一头牛只能得到一个食物和饮料。
而且一头牛必须同时获得一个食物和一个饮料才能满足。问至多有多少头牛可以获得满足。
 
思路:最大流建图,把食物和饮料放在两端。一头牛拆分成两个点,两点之间的容量为1.喜欢的食物和饮料跟牛建条边,容量为1.
加个源点和汇点。源点与食物、饮料和汇点的边容量都是1,表示每种食物和饮料只有一个。

建图才是关键啊

源点-->food-->牛(左)-->牛(右)-->drink-->汇点

牛拆点,确保一头牛就选一套food和drink的搭配

  1 //2017-08-23
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8 
  9 using namespace std;
 10 
 11 const int N = 510;
 12 const int INF = 0x3f3f3f3f;
 13 int head[N], tot;
 14 struct Edge{
 15     int next, to, w;
 16 }edge[N<<4];
 17 
 18 void add_edge(int u, int v, int w){
 19     edge[tot].w = w;
 20     edge[tot].to = v;
 21     edge[tot].next = head[u];
 22     head[u] = tot++;
 23 
 24     edge[tot].w = 0;
 25     edge[tot].to = u;
 26     edge[tot].next = head[v];
 27     head[v] = tot++;
 28 }
 29 
 30 struct Dinic{
 31     int level[N], S, T;
 32     void init(int _S, int _T){
 33         S = _S;
 34         T = _T;
 35         tot = 0;
 36         memset(head, -1, sizeof(head));
 37     }
 38     bool bfs(){
 39         queue<int> que;
 40         memset(level, -1, sizeof(level));
 41         level[S] = 0;
 42         que.push(S);
 43         while(!que.empty()){
 44             int u = que.front();
 45             que.pop();
 46             for(int i = head[u]; i != -1; i = edge[i].next){
 47                 int v = edge[i].to;
 48                 int w = edge[i].w;
 49                 if(level[v] == -1 && w > 0){
 50                     level[v] = level[u]+1;
 51                     que.push(v);
 52                 }
 53             }
 54         }
 55         return level[T] != -1;
 56     }
 57     int dfs(int u, int flow){
 58         if(u == T)return flow;
 59         int ans = 0, fw;
 60         for(int i = head[u]; i != -1; i = edge[i].next){
 61             int v = edge[i].to, w = edge[i].w;
 62             if(!w || level[v] != level[u]+1)
 63                   continue;
 64             fw = dfs(v, min(flow-ans, w));
 65             ans += fw;
 66             edge[i].w -= fw;
 67             edge[i^1].w += fw;
 68             if(ans == flow)return ans;
 69         }
 70         if(ans == 0)level[u] = 0;
 71         return ans;
 72     }
 73     int maxflow(){
 74         int flow = 0;
 75         while(bfs())
 76           flow += dfs(S, INF);
 77         return flow;    
 78     }
 79 }dinic;
 80 
 81 int main()
 82 {
 83     std::ios::sync_with_stdio(false);
 84     //freopen("inputB.txt", "r", stdin);
 85     int n, f, d;
 86     while(cin>>n>>f>>d){
 87         int s = 0, t = 2*n+f+d+2;
 88         dinic.init(s, t);
 89         for(int i = 1; i <= n; i++)
 90               add_edge(i, n+i, 1);
 91         for(int i = 1; i <= f; i++)
 92               add_edge(s, 2*n+i, 1);
 93         for(int i = 1; i <= d; i++)
 94               add_edge(2*n+f+i, t, 1);
 95         int nf, nd, v;
 96         for(int i = 1; i <= n; i++){
 97             cin>>nf>>nd;
 98             for(int j = 0; j < nf; j++){
 99                 cin>>v;
100                 add_edge(2*n+v, i, 1);
101             }
102             for(int j = 0; j < nd; j++){
103                 cin>>v;
104                 add_edge(n+i, 2*n+f+v, 1);
105             }
106         }
107         cout<<dinic.maxflow()<<endl;
108     }    
109     return 0;
110 }
原文地址:https://www.cnblogs.com/Penn000/p/7418243.html