Max Stream Augmentation Path (km algorithm) hdoj 2255 Ben-off make money

Source: Internet
Author: User

Topic Portal

1 /*2 KM: Bare topic The first way, it seems to be the upgrade version of Hungary, not good understanding, write some comments3 km algorithm is used to solve the maximum weight matching problem: In a binary graph, the left vertex is x, the right vertex is Y, now for each group of right and left connected Xi,yj entitled W (i,j),4 find a match that makes all W (i,j) and Max. That is, the maximum weight match must be a complete match. A perfect match if the points on both sides are equal. 5 if the points are not equal, you can actually virtual some points, making the points equal, also become a perfect match. The most powerful match can also be solved with the maximum flow.6 */7#include <cstdio>8#include <algorithm>9#include <cstring>Ten using namespacestd; One  A Const intMAXN = 3e2 +Ten; - Const intINF =0x3f3f3f3f; - intX[MAXN], Y[MAXN], W[MAXN][MAXN]; the intLX[MAXN], LY[MAXN]; - BOOLVISX[MAXN], VISY[MAXN]; - intN, D; -  + BOOLDFS (intu) {//Hungary Algorithm -Visx[u] =true; +      for(intI=1; i<=n; ++i) { A         if(X[u] + y[i] = = W[u][i] &&!Visy[i]) { atVisy[i] =true; -             if(Ly[i] = =-1||DFS (Ly[i])) { -Ly[i] = u;return true; -             } -         } -         Else if(X[u] + y[i] > w[u][i]) d = min (d, x[u] + y[i]-w[u][i]);//Update D, greedy thoughts in     } -  to     return false; + } -  the voidKM (void)    { *      for(intI=1; i<=n; ++i) { $X[i] =0;Panax Notoginseng          for(intj=1; j<=n; ++j) { -X[i] = max (X[i], w[i][j]);//the initial X benchmark is the maximum value of w,y 0 the         } +     } A  thememset (Y,0,sizeof(y)); +memset (Ly,-1,sizeof(ly)); -      for(intI=1; i<=n; ++i) { $          while(true)    { $memset (VISX,false,sizeof(VISX)); -memset (Visy,false,sizeof(Visy)); -D =INF; the             if(DFS (i)) Break;//Find the augmented orbit, exit -              for(intI=1; i<=n; ++i) {//not found, adjusted for the benchmarkWuyi                 if(Visx[i]) x[i]-=D; the                 if(Visy[i]) y[i] + =D; -             } Wu         } -     } About  $     intres =0; -      for(intI=1; i<=n; ++i) { -Res + = X[i] +Y[i]; -     } Aprintf ("%d\n", res); + } the  - intMainvoid)    {//hdoj 2255 Ben-off make money $     //freopen ("hdoj_2255.in", "R", stdin); the  the      while(SCANF ("%d", &n) = =1)    { the          for(intI=1; i<=n; ++i) { the              for(intj=1; j<=n; ++j) { -scanf ("%d", &w[i][j]); in             } the         } the KM (); About     } the  the     return 0; the}

Max Stream Augmentation Path (km algorithm) hdoj 2255 Ben-off make money

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.