The Hungarian algorithm was proposed by the Hungarian mathematician Edmonds in 1965, hence the name. The Hungarian algorithm is based on the idea of sufficiency proof in Hall theorem, it is the most common algorithm of the part graph matching, and the core of the algorithm is to find the augmented path, which is an algorithm for finding the maximal matching of binary graph with the augmented path.
Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2063
Main topic:
The Chinese topic, the point in the immediately know.
Problem Solving Ideas:
The problem is to find the maximum number of matches, directly apply the Hungarian algorithm template, the algorithm is probably the principle is: there is opportunity, no opportunity to create opportunities to go on.
Code:
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 7 #defineMAXN 5058 intMAP[MAXN][MAXN], USED[MAXN], GIRL[MAXN], N, m;9 //map Storage matching relationship, used used to mark whether or not augmented, girl[i] means girl[i] This girl has been matched with I, the boy .Ten BOOLFind (intx); One intMain () A { - intK; - while(SCANF ("%d", &k), K) the { -scanf ("%d%d", &m, &n); -memset (Map,0,sizeof(map)); -Memset (Girl,0,sizeof(Girl)); + - while(K--) + { A intu, v; atscanf ("%d%d", &u, &v); -MAP[U][V] =1; - } - - intsum =0; - for(intI=1; i<=m; i++) in{//each girl match, need to empty tag augmented array -memset (Used,0,sizeof(used)); to if(Find (i)) +Sum + +; - } theprintf ("%d\n", sum); * } $ return 0;Panax Notoginseng } - the BOOLFind (intx) +{//Dfs Augmentation, finding the match for x A for(intI=1; i<=n; i++) the { + if(!used[i] &&Map[x][i]) -{//not augmented and X can be matched with I $Used[i] =1; $ if(!girl[i] | |find (Girl[i])) -{//I did not match any of the girls or found the Grace Road, then the match succeeded -Girl[i] =x; the return true; - }Wuyi } the } - return false; Wu}
HDU 2063 roller coaster (maximum matching Hungarian algorithm template)