HDU 1150 - Machine Schedule
今天熟悉了一下匈牙利算法。简单来说,匈牙利算法解决的问题就是二分图的无权完备匹配问题,关于带权完备匹配参见Kuhn–Munkres algorithm.
关于这方面的知识,主要的参考资料是ByVoid的两篇:
https://www.byvoid.com/blog/hungary/
https://www.byvoid.com/blog/match-km/
Wikipedia
http://en.wikipedia.org/wiki/Hungarian_algorithm
还有Harold W. Kuhn的一篇论文
The Hungarian Method for the assignment problem
1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:hdu1150 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 using namespace std; 11 12 #define MAXN 107 13 #define MAXM 107 14 15 int N, M; 16 int visited[MAXM]; 17 int matched[MAXM]; 18 19 20 int e[MAXN][MAXN]; 21 22 int aug(int i) 23 { 24 for(int j=0; j<M; ++j) { 25 if (e[i][j] && !visited[j]) { 26 visited[j] = 1; 27 if (matched[j] < 0 || aug(matched[j])) { 28 matched[j] = i; 29 return 1; 30 } 31 } 32 } 33 return 0; 34 } 35 36 int main(void) 37 { 38 #ifndef ONLINE_JUDGE 39 freopen("in.txt", "r", stdin); 40 #endif 41 42 int K; 43 while(scanf("%d%d%d", &N, &M, &K) >= 3) { 44 memset(e, 0, sizeof(e)); 45 for(int i=0; i<K; ++i) { 46 int _, x, y; 47 scanf("%d%d%d", &i, &x, &y); 48 if (x > 0 && y > 0) e[x][y] = 1; 49 } 50 memset(matched, -1, sizeof(matched)); 51 // hungrian method 52 int m = 0; 53 for(int i=0; i<N; ++i) { 54 m += aug(i); 55 memset(visited, 0, sizeof(visited)); 56 } 57 printf("%d\n", m); 58 } 59 return 0; 60 }
2014-09-17 02:09:32 | Accepted | 1150 | 0MS | 352K | 972 B | G++ |
This article is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
本文采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。