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