KM algorithm of nudity. O (n^4) template , in fact, in the augmented path is still redundant, you can use BFS optimization to O (n^3).
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <cstdlib>6#include <algorithm>7 using namespacestd;8 Const intmaxn= -+5;9 Const intinf=0x7fffffff;Ten intn,m; One intW[MAXN][MAXN]; A intX[MAXN],Y[MAXN]; - intVISX[MAXN],VISY[MAXN],SLACK[MAXN]; - intLK[MAXN]; the - intDfsintu) - { -visx[u]=1; + for(intI=1; i<=n;i++) - { + intwt=x[u]+y[i]-W[u][i]; A if(!visy[i] && wt==0) at { -visy[i]=1; - if(lk[i]==-1||DFS (Lk[i])) - { -lk[i]=u; - return 1; in } - } to Else if(SLACK[I]>WT) slack[i]=wt; + } - return 0; the } * $ intKM ()Panax Notoginseng { -memset (lk,-1,sizeof(LK)); the for(intI=1; i<=n;i++) + { Ay[i]=0; thex[i]=-INF; + for(intj=1; j<=n;j++) x[i]=Max (x[i],w[i][j]); - } $ for(intI=1; i<=n;i++) $ { -memset (VISX,0,sizeof(VISX)); -memset (Visy,0,sizeof(Visy)); thememset (Slack,127,sizeof(slack)); - while(!DFS (i))Wuyi { the intDelta=INF; - for(intj=1; j<=n;j++) Wu if(!visy[j] && Slack[j]<delta) delta=Slack[j]; - for(intj=1; j<=n;j++) About { $ if(Visx[j]) - { -x[j]-=Delta; -visx[j]=0; A } + if(Visy[j]) the { -y[j]+=Delta; $visy[j]=0; the } the } the } the } - intret=0; in for(intI=1; i<=n;i++) ret+=x[i]+Y[i]; the returnret; the } About the voidInit () the { theMemset (W,0,sizeof(w)); + for(intI=1; i<=n;i++) - { the for(intj=1; j<=n;j++) Bayiscanf"%d",&w[i][j]); the } the } - - intMain () the { the while(~SCANF ("%d",&N)) the { the init (); -printf"%d\n", KM ()); the } the return 0; the}
"km algorithm" hdu2255-Ben-off make money