hdu2255-Ben well-off make a lot of money-two graph maximum weight matching-km algorithm

Source: Internet
Author: User

The maximum weight matching problem of binary graph. Using the KM algorithm.

The weight value is set to the opposite number when the minimum weight is

1 /*--------------------------------------------------------------------------------------*/2 3#include <algorithm>4#include <iostream>5#include <cstring>6#include <ctype.h>7#include <cstdlib>8#include <cstdio>9#include <vector>Ten#include <string> One#include <queue> A#include <stack> -#include <cmath> -#include <Set> the#include <map> -  - //Debug function for a n*m array - #defineDebug_map (n,m,g) printf ("\ n"); for (int i=0;i< (N); i++) +{ for(intj=0;j< (M); J + +){ -printf"%d", G[i][j]);} printf"\ n");} + //Debug function for int,float,double,etc. A #defineDebug_var (X) cout<< #X "=" <<X<<endl; at #defineLL Long Long - Const intINF =0x3f3f3f3f; - ConstLL Llinf =0x3f3f3f3f3f3f3f3f; - /*--------------------------------------------------------------------------------------*/ - using namespacestd; -  in intn,m,t; - Const intMAXN =310; to intNx,ny; + intG[MAXN][MAXN]; - intLINKER[MAXN],LX[MAXN],LY[MAXN]; the intSLACK[MAXN]; * BOOLVISX[MAXN],VISY[MAXN]; $ BOOLDFS (intx)Panax Notoginseng { -VISX[X] =true; the      for(inty=0; y<ny;y++) +     { A         if(Visy[y])Continue; the         intTMP = Lx[x] + ly[y]-G[x][y]; +         if(TMP = =0) -         { $Visy[y] =true; $             if(Linker[y] = =-1||DFS (Linker[y])) -             { -Linker[y] =x; the                 return true; -             }Wuyi         } the         Else if(Slack[y] > tmp) slack[y] =tmp; -     } Wu     return false; - } About intKM () $ { -memset (linker,-1,sizeoflinker); -memset (Ly,0,sizeofly); -      for(intI=0; i<nx;i++) A     { +Lx[i] =-INF; the          for(intj=0; j<ny;j++) Lx[i] =Max (lx[i],g[i][j]); -     } $      for(intx=0; x<nx;x++) the     { the          for(intI=0; i<ny;i++) Slack[i] =INF; the          while(true) the         { -memset (VISX,false,sizeofvisx); inmemset (Visy,false,sizeofVisy); the             if(DFS (x)) Break; the             intD =INF; About              for(intI=0; i<ny;i++)if(!visy[i] && d > slack[i]) d =Slack[i]; the              for(intI=0; i<nx;i++)if(Visx[i]) lx[i]-=D; the              for(intI=0; i<ny;i++) the             { +                 if(Visy[i]) ly[i] + =D; -                 ElseSlack[i]-=D; the             }Bayi         } the     } the     intres =0; -      for(intI=0; i<ny;i++)if(Linker[i]! =-1) Res + =G[linker[i]][i]; -     returnRes; the } the intMain () the { the      while(~SCANF ("%d",&N)) -     { the          for(intI=0; i<n;i++) the         { the              for(intj=0; j<n;j++)94             { thescanf"%d",&g[i][j]); the             } the         }98NX = NY =N; Aboutprintf"%d\n", KM ()); -     }101}

hdu2255-Ben well-off make a lot of money-two graph maximum weight matching-km algorithm

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.