HDU 2063 roller coaster (maximum matching Hungarian algorithm template)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.