HDU 2063 过山车(匈牙利算法)

过山车

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 48   Accepted Submission(s) : 23

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了。可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐。但是,每个女孩都有各自的想法,举个例子把,Rabbit只愿意和XHD或PQK做partner,Grass只愿意和linle或LL做partner,PrincessSnow愿意和水域浪子或伪酷儿做partner。考虑到经费问题,boss刘决定只让找到partner的人去坐过山车,其他的人,嘿嘿,就站在下面看着吧。聪明的Acmer,你可以帮忙算算最多有多少对组合可以坐上过山车吗?

Input

输入数据的第一行是三个整数K , M , N,分别表示可能的组合数目,女生的人数,男生的人数。0<K<=1000
1<=N 和M<=500.接下来的K行,每行有两个数,分别表示女生Ai愿意和男生Bj做partner。最后一个0结束输入。

Output

对于每组数据,输出一个整数,表示可以坐上过山车的最多组合数。

Sample Input

6 3 3
1 1
1 2
1 3
2 1
2 3
3 1
0

Sample Output

3

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 #include <cstdio>
 6 using namespace std;
 7 int k, m, n;
 8 bool v[505];
 9 bool e[505][505];
10 int pl[505];
11 bool find(int x)
12 {
13     int i;
14     for (i = 1; i <= n; i++)
15     {
16         if (e[x][i] &&!v[i])
17         {
18             v[i] = 1;
19             if (pl[i] == 0 || find(pl[i]))//没有伴侣或者能为伴侣找到其它伴侣
20             {
21                 pl[i] = x;
22                 return 1;
23             }
24         }
25     }
26     return 0;
27 }
28 int main()
29 {
30     while (cin >> k && k)
31     {
32         memset(e, 0, sizeof(e));
33         memset(pl, 0, sizeof(pl));
34         cin >> m >> n;//m个女生,n个男生
35         int i;
36         for (i = 1; i <= k; i++)
37         {
38             int x, y;
39             cin >> x >> y;
40             e[x][y] = 1;
41         }
42         int ans = 0;
43         for (i = 1; i <= m; i++)
44         {
45             memset(v, 0, sizeof(v)); //这个在每一步中清空 
46             if (find(i)) ans++;
47         }
48         cout << ans << endl;
49     }
50 }
原文地址:https://www.cnblogs.com/caiyishuai/p/8439593.html