Topic Portal
1 /*2 minimum cost flow: km algorithm is to find the maximum flow, as long as W =-W can be, very classical 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]; - intMX[MAXN], MY[MAXN]; - intHX[MAXN], HY[MAXN]; - CharMAZE[MAXN][MAXN]; + intN, M, UN, vn, D; - + BOOLDFS (intu) { AVisx[u] =true; at for(intI=1; i<=un; ++i) { - if(!visy[i] && X[u] + y[i] = =W[u][i]) { -Visy[i] =true; - if(Ly[i] = =-1||DFS (Ly[i])) { -Ly[i] = u;return true; - } in } - Else if(X[u] + y[i] > w[u][i]) d = min (d, x[u] + y[i]-w[u][i]); to } + return false; - } the * intKM (void) { $ for(intI=1; i<=un; ++i) {Panax NotoginsengX[i] =-INF; - for(intj=1; j<=vn; ++j) { theX[i] =Max (X[i], w[i][j]); + } A } the +memset (Ly,-1,sizeof(ly)); -memset (Y,0,sizeof(y)); $ for(intI=1; i<=un; ++i) { $ while(true) { -memset (VISX,false,sizeof(VISX)); -memset (Visy,false,sizeof(Visy)); theD =INF; - if(DFS (i)) Break;Wuyi for(intI=1; i<=un; ++i) { the if(Visx[i]) x[i]-=D; - } Wu for(intj=1; j<=vn; ++j) { - if(Visy[j]) y[j] + =D; About } $ } - } - - intres =0; A for(intI=1; i<=un; ++i) { +Res + = X[i] +Y[i]; the } - $ returnRes; the } the the intMainvoid) {//hdoj 1533 going Home the //freopen ("hdoj_1533.in", "R", stdin); - in while(SCANF ("%d%d", &n, &m) = =2) { the if(!n &&!m) Break; the for(intI=1; i<=n; ++i) { Aboutscanf ("%s", Maze[i] +1); the } theUn = VN =0; the for(intI=1; i<=n; ++i) { + for(intj=1; j<=m; ++j) { - if(Maze[i][j] = ='m') Mx[++un] = i, my[un] =J; the Else if(Maze[i][j] = ='H') HX[++VN] = i, hy[vn] =J;Bayi } the } the for(intI=1; i<=un; ++i) { - for(intj=1; j<=vn; ++j) { -W[I][J] =-(ABS (Mx[i]-hx[j]) + ABS (my[i)-hy[j])); the } the } theprintf ("%d\n", -KM ()); the } - the return 0; the}
Max Stream Augmentation Path (km algorithm) hdoj 1533 going Home