Topic Portal
1 /*2 KM: Compared to hdoj_1533, more heavy-edge processing, and a perfect match to determine the method3 */4#include <cstdio>5#include <cmath>6#include <algorithm>7#include <cstring>8 using namespacestd;9 Ten Const intMAXN = 1e2 +Ten; One Const intINF =0x3f3f3f3f; A intX[MAXN], Y[MAXN]; - intW[MAXN][MAXN]; - intVISX[MAXN], VISY[MAXN]; the intLY[MAXN]; - intN, M, D; - - BOOLDFS (intu) { +Visx[u] =true; - for(intI=1; i<=n; ++i) { + if(!visy[i] && X[u] + y[i] = =W[u][i]) { AVisy[i] =true; at if(Ly[i] = =-1||DFS (Ly[i])) { -Ly[i] = u;return true; - } - } - Else if(X[u] + y[i] > w[u][i]) d = min (d, x[u] + y[i]-w[u][i]); - } in return false; - } to + intKM (void) { - for(intI=1; i<=n; ++i) { theX[i] =-INF; * for(intj=1; j<=n; ++j) { $X[i] =Max (X[i], w[i][j]); Panax Notoginseng } - } the +memset (Ly,-1,sizeof(ly)); Amemset (Y,0,sizeof(y)); the for(intI=1; i<=n; ++i) { + while(true) { -memset (VISX,false,sizeof(VISX)); $memset (Visy,false,sizeof(Visy)); $D =INF; - if(DFS (i)) Break; - for(intI=1; i<=n; ++i) { the if(Visx[i]) x[i]-=D; - }Wuyi for(intj=1; j<=n; ++j) { the if(Visy[j]) y[j] + =D; - } Wu } - } About $ for(intI=1; i<=n; ++i) { - if(Ly[i] = =-1|| W[ly[i]][i] = =-inf)return-1; - } - A intres =0; + for(intI=1; i<=n; ++i) { theRes + = X[i] +Y[i]; - } $ the return-Res; the } the the intMainvoid) {//hdoj 1853 Cyclic Tour - //freopen ("hdoj_1853.in", "R", stdin); in the while(SCANF ("%d%d", &n, &m) = =2) { the if(!n &&!m) Break; About for(intI=1; i<=n; ++i) { the for(intj=1; j<=n; ++J) W[i][j] =-INF; the } the for(intI=1; i<=m; ++i) { + intU, V, c; scanf ("%d%d%d", &u, &v, &c); - if(-c > W[u][v]) w[u][v] =-C; the }Bayiprintf ("%d\n", KM ()); the } the - return 0; -}
Max Stream Augmentation Path (km algorithm) HDOJ 1853 Cyclic Tour