Maximum binary graph matching, O (n^3).
1 /*2255*/2#include <iostream>3#include <algorithm>4#include <cstdio>5#include <cstring>6#include <cstdlib>7 using namespacestd;8 9 #defineMAXN 305Ten #defineINF 0XFFFFFFF One A intW[MAXN][MAXN]; - intLINK[MAXN]; - intLX[MAXN], LY[MAXN]; the intSlack; - BOOLS[MAXN], T[MAXN]; - intN; - + BOOLDfsinti) { -S[i] =true; + for(intj=1; j<=n; ++j) { A if(T[j]) at Continue; - intTMP = lx[i]+ly[j]-W[i][j]; - if(TMP = =0) { -T[J] =true; - if(!link[j] | |DFS (Link[j])) { -LINK[J] =i; in return true; - } to}Else if(Slack >tmp) { +Slack =tmp; - } the } * return false; $ }Panax Notoginseng - voidUpdate () { the for(intI=1; i<=n; ++i) { + if(S[i]) Lx[i]-=Slack; A if(T[i]) Ly[i] + =Slack; the } + } - $ voidKM () { $ intI, J, K; - -memset (Link,0,sizeof(link)); thememset (Lx,0,sizeof(Lx)); -memset (Ly,0,sizeof(Ly));Wuyi for(i=1; i<=n; ++i) the for(j=1; j<=n; ++j) -Lx[i] =Max (Lx[i], w[i][j]); Wu for(i=1; i<=n; ++i) { - for (;;) { Aboutmemset (S),false,sizeof(S)); $memset (T,false,sizeof(T)); -Slack =INF; - if(Dfs (i)) - Break; A Else + update (); the } - } $ } the the intMain () { the intI, J, K; the intans; - in #ifndef Online_judge theFreopen ("data.in","R", stdin); the #endif About the while(SCANF ("%d", &n)! =EOF) { the for(i=1; i<=n; ++i) the for(j=1; j<=n; ++j) +scanf"%d", &w[i][j]); - KM (); the for(ans=0, i=1; i<=n; ++i)Bayi if(Link[i]) theAns + =W[link[i]][i]; theprintf"%d\n", ans); - } - the return 0; the}
"Hdoj" 2255 Ben-off make money