"Binary graph Maximum weight perfect match" "km algorithm template" "Go"

Source: Internet
Author: User

"Article in detail source" https://www.cnblogs.com/wenruo/p/5264235.html

KM algorithm is used to find the maximum weight of the binary graph perfect match. " also even if the previous Hungarian algorithm to find the best binary matching variant?? "

This is where the template code is posted. 2333 ...

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 Const intMAXN =305;7 Const intINF =0x3f3f3f3f;8 9 intLOVE[MAXN][MAXN];//record the goodwill of every sister and every boyTen intEX_GIRL[MAXN];//the expectations of every sister One intEX_BOY[MAXN];//the expectations of every boy A BOOLVIS_GIRL[MAXN];//record every match of the match Girl - BOOLVIS_BOY[MAXN];//keep track of each match, match the boys. - intMATCH[MAXN];//record each boy's match to the sister, if not, 1. the intSLACK[MAXN];//keep track of the number of expectations that every man needs at least if he can be attracted to a sister -  - intN; -  +  - BOOLDfsintgirl) + { AVis_girl[girl] =true; at  -      for(intBoy =0; Boy < N; ++Boy ) { -  -         if(Vis_boy[boy])Continue;//each round matches each boy only try once -  -         intGap = Ex_girl[girl] + ex_boy[boy]-Love[girl][boy]; in  -         if(Gap = =0) {//If the requirements are met toVis_boy[boy] =true; +             if(Match[boy] = =-1|| DFS (Match[boy])) {//find a guy who doesn't have a match, or the boy's sister can find someone else . -Match[boy] =girl; the                 return true; *             } $}Else {Panax NotoginsengSlack[boy] = min (Slack[boy], GAP);//slack can be understood as the boy who wants to get a girl's heart. How much will it take to have a minimum spare tire appearance "cover your face -         } the     } +  A     return false; the } +  - intKM () $ { $memset (Match,-1,sizeofMatch);//Initially, every guy doesn't have a matching girl. -memset (Ex_boy,0,sizeofEx_boy);//the initial expectation for each boy is 0. -  the     //The initial expectation of each girl is the greatest affection of the boy who is connected to her . -      for(inti =0; i < N; ++i) {WuyiEx_girl[i] = love[i][0]; the          for(intj =1; J < N; ++j) { -Ex_girl[i] =Max (Ex_girl[i], love[i][j]); Wu         } -     } About  $     //try to solve a problem for every girl -      for(inti =0; i < N; ++i) { -  -Fill (slack, slack + N, INF);//because to take the minimum value to initialize to infinity A  +          while(1) { the             //The way to solve the problem for each girl is to reduce the expectation if it is not found. -  $             //keep track of whether the boys and girls are trying to match each round . thememset (Vis_girl,false,sizeofvis_girl); thememset (Vis_boy,false,sizeofvis_boy); the  the             if(Dfs (i)) Break;//find a destination to exit -  in             //If you can't find it, lower your expectations . the             //minimum expected value to reduce the             intD =INF; About              for(intj =0; J < N; ++j) the                 if(!vis_boy[j]) d =min (d, slack[j]); the  the              for(intj =0; J < N; ++j) { +                 //all girls who have visited lower their expectations -                 if(Vis_girl[j]) ex_girl[j]-=D; the Bayi                 //all the boys visited increased their expectations. the                 if(Vis_boy[j]) ex_boy[j] + =D; the                 //the boy who has not visited because the expectations of the girl lower, the distance to get a girl fell in a step!  -                 ElseSLACK[J]-= D;//in fact, I feel this step does not matter, anyway after the slack[boy] = min (Slack[boy], GAP); update slack[j]. -             } the         } the     } the  the     //match completion to find out all pairs of goodwill and -     intres =0; the      for(inti =0; i < N; ++i) theRes + =love[Match[i]][i]; the 94     returnRes; the } the  the intMain ()98 { About      while(~SCANF ("%d", &N)) { - 101          for(inti =0; i < N; ++i)102              for(intj =0; J < N; ++j)103scanf"%d", &love[i][j]);104  theprintf"%d\n", KM ());106     }107     return 0;108}

"Binary graph Maximum weight perfect match" "km algorithm template" "Go"

Related Article

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.