18.11.26 POJ 1274 The Perfect Stall(二部图最大匹配/网络流)

描述

Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall. 
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible. 
输入

The input includes several cases. For each case, the first line contains two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. Each of the following N lines corresponds to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow.

输出

For each case, output a single line with a single integer, the maximum number of milk-producing stall assignments that can be made.

样例输入

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2 

样例输出

4

来源

USACO 40

这题我用了两种写法,因为我在写完网络流才想起来有匈牙利算法……

网络流(dicnic):

  1 #include <iostream>
  2 #include <string.h>
  3 #include <algorithm>
  4 #include <stack>
  5 #include <string>
  6 #include <math.h>
  7 #include <queue>
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <vector>
 11 #include <fstream>
 12 #include <set>
 13 #define maxn 505
 14 #define inf 999999
 15 
 16 using namespace std;
 17 int Layer[maxn], G[maxn][maxn];
 18 bool visited[maxn];
 19 int n, m, Size,t;
 20 
 21 bool countlayer() {
 22     deque<int>q;
 23     int layer = 0;
 24     memset(Layer, 0xff, sizeof(Layer));
 25     Layer[0] = 0; q.push_back(0);
 26     while (!q.empty()) {
 27         int v = q.front(); q.pop_front();
 28         for (int i = 0; i <= t; i++) {
 29             if (G[v][i] > 0 && Layer[i] == -1) {
 30                 Layer[i] = Layer[v] + 1;
 31                 if (i == t)return true;
 32                 q.push_back(i);
 33             }
 34         }
 35     }
 36     return false;
 37 }
 38 
 39 void dicnic() {
 40     int i, s;
 41     int flow = 0;
 42     deque<int>q;
 43     while (countlayer()) {
 44         q.push_back(0);
 45         memset(visited, 0, sizeof(visited));
 46         visited[0] = true;
 47         while (!q.empty()) {
 48             int nd = q.back();
 49             int nminc = inf, nminc_vs;
 50             if (nd == t) {
 51                 for (i = 1; i < q.size(); i++) {
 52                     int vs = q[i - 1];
 53                     int ve = q[i];
 54                     if (G[vs][ve] < nminc) {
 55                         nminc = G[vs][ve];
 56                         nminc_vs = vs;
 57                     }
 58                     G[vs][ve]--;
 59                     G[ve][vs]++;
 60                 }
 61                 flow++;
 62                 while (!q.empty()&&q.back() != nminc_vs) {
 63                     visited[q.back()] = 0;
 64                     q.pop_back();
 65                 }
 66             }
 67             else {
 68                 for (i = 0; i <= t; i++) {
 69                     if (G[nd][i] > 0 && Layer[i] == Layer[nd] + 1 && !visited[i]) {
 70                         visited[i] = true;
 71                         q.push_back(i);
 72                         break;
 73                     }
 74                 }
 75                 if (i > t)
 76                     q.pop_back();
 77             }
 78         }
 79     }
 80     printf("%d
", flow);
 81 }
 82 
 83 void init() {
 84     memset(G, 0, sizeof(G));
 85     Size = n + m;
 86     t = Size + 1;
 87     for (int i = 1; i <= n; i++) {
 88         G[0][i] = 1;
 89         int x;
 90         scanf("%d", &x);
 91         while (x--) {
 92             int stall;
 93             scanf("%d", &stall);
 94             G[i][n + stall] = 1;
 95         }
 96     }
 97     for (int i = n + 1; i <= Size; i++)
 98         G[i][t] = 1;
 99     dicnic();
100 }
101 
102 int main()
103 {
104     while(scanf("%d%d", &n, &m) != EOF)
105         init();
106     return 0;
107 }
View Code

匈牙利算法(我喜欢这个名字):

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #include <set>
13 #define maxn 505
14 #define inf 999999
15 
16 using namespace std;
17 bool  G[maxn][maxn];
18 bool visited[maxn];
19 int Pair[maxn];
20 int n, m, Size,t;
21 
22 int find(int x) {
23     for (int i = 1; i <= m; i++) {
24         if (G[x][i] && !visited[i]) {
25             visited[i] = true;
26             if (Pair[i] == 0 || find(Pair[i])) {
27                 Pair[i] = x;
28                 return true;
29             }
30         }
31     }
32     return false;
33 }
34 
35 void init() {
36     memset(G, 0, sizeof(G));
37     memset(Pair, 0, sizeof(Pair));
38     Size = n + m;
39     t = Size + 1;
40     for (int i = 1; i <= n; i++) {
41         int x;
42         scanf("%d", &x);
43         while (x--) {
44             int stall;
45             scanf("%d", &stall);
46             G[i][stall] = true;
47         }
48     }
49     int res = 0;
50     for (int i = 1; i <= m; i++) {
51         memset(visited, 0, sizeof(visited));
52         if (find(i))res++;
53     }
54     printf("%d
", res);
55 }
56 
57 int main()
58 {
59     while(scanf("%d%d", &n, &m) != EOF)
60         init();
61     return 0;
62 }
View Code

本来想贴个匈牙利算法的证明,但那个帖子也年久失修了……

记录一下自己的理解,如果错了的话希望还能改正……

也有种贪心的意味在里面,一个个把点加进去的过程中,如果发现想要连接的点已经被别的点连了,就让占着当前点的那个点挪位子,这样总是最优的(好像有点口胡,差不多这意思)

注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
原文地址:https://www.cnblogs.com/yalphait/p/10022380.html