Encyclopedia: Http://baike.baidu.com/link?url=vbM3H4XmfrsWfP-epdlR2sVKSNzOq4hXnWDqm5uo8fd7VWsF2SmhDV35XyVUDvVjvrtf42RUITJuNCHn-7_x6K
Great God Summary: http://www.cnblogs.com/skyming/archive/2012/02/18/2356919.html
Code:
1#include <stdio.h>2#include <string.h>3 Const intn=555, inf=0x3f3f3f3f;4 intLx[n],ly[n],vx[n],vy[n],slack[n],match[n];5 intA[n][n];6 intNx,ny;7 intDfsintu)8 {9vx[u]=1;Ten for(intI=1; i<=ny;i++) One { A if(Vy[i])Continue; - intt=lx[u]+ly[i]-A[u][i]; - if(t==0) the { -vy[i]=1; - if(match[i]==-1||DFS (Match[i])) - { +match[i]=u; - return 1; + } A } at Else if(slack[i]>t) -slack[i]=T; - } - return 0; - } - intKM () in { - inti,j,d; tomemset (Ly,0,sizeof(ly)); +memset (match,-1,sizeof(Match)); - for(i=1, lx[i]=-inf;i<=nx;i++) the for(j=1; j<=ny;j++) * if(a[i][j]>Lx[i]) $lx[i]=A[i][j];Panax Notoginseng for(i=1; i<=nx;i++) - { thememset (Slack,0x3f,sizeof(slack)); + while(1) A { thememset (VX,0,sizeof(VX)); +memset (VY,0,sizeof(VY)); - if(Dfs (i)) Break; $D=INF; $ for(j=1; j<=ny;j++) - if(!vy[j]&&slack[j]<d) -D=Slack[j]; the //if (d==inf) break;//There is no match for this point - for(j=1; j<=nx;j++)Wuyi if(Vx[j]) thelx[j]-=D; - for(j=1; j<=ny;j++) Wu { - if(Vy[j]) Aboutly[j]+=D; $ Else -slack[j]-=D; - } - } A } + intans=0, sum=0; the for(i=1; i<=ny;i++) - if(match[i]>-1&&A[match[i]][i]) $ans+=a[match[i]][i],sum++; theprintf"%d++\n", sum);//Number of matches the returnans; the } the intMain () - { in intm,u,v,w,n,i; thescanf"%d",&NX); thescanf"%d",&NY); Aboutscanf"%d",&m); theMemset (A,0,sizeof(a)); the while(m--) the { +scanf"%d%d%d",&u,&v,&W); -a[u][v]=a[v][u]=W; the }Bayiprintf"%d\n", KM ()); the return 0; the}
Matching algorithm of the best power value for binary matching---km templates