The Kuhn-munkres algorithm, the first shot of a variety of problems, but a fell ..
/* ID: esxgx1lang: C ++ prog: hdu2255 */# include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; typedef long ll; # define lxn 307 # define rxn 307int W [lxn] [rxn]; int lx [lxn], Ly [rxn]; char visx [lxn], visy [rxn]; int matched [rxn], slack [rxn]; int N; # define ln n # define rn nint Aug (int I) {visx [I] = 1; for (Int J = 0; j <rn; ++ J) {If (! Visy [J]) {int d = Lx [I] + ly [J]-W [I] [J]; If (! D) {visy [J] = 1; if (matched [J] <0 | Aug (matched [J]) {matched [J] = I; return 1 ;}} else if (Slack [J]> d) slack [J] = D ;}} return 0 ;}# define INF 0x3f3f3fvoid work (void) {for (INT I = 0; I <ln; ++ I) {lx [I] = W [I] [0]; for (Int J = 1; j <rn; ++ J) if (W [I] [J]> lx [I]) lx [I] = W [I] [J];} for (INT I = 0; I <rn; ++ I) ly [I] = 0; memset (matched,-1, sizeof (matched )); // Kuhn-munkres for (INT I = 0; I <ln; ++ I) {For (Int J = 0; j <rn; ++ J) slack [J] = inf; while (1) {memset (visx, 0, sizeof (visx )); memset (visy, 0, sizeof (visy); If (Aug (I) break; int D, j = 0; For (; j <rn; ++ J) if (! Visy [J]) {d = J; break;} For (; j <rn; ++ J) if (! Visy [J] & slack [d]> slack [J]) d = J; D = slack [d]; for (Int J = 0; j <ln; ++ J) if (visx [J]) lx [J]-= D; For (Int J = 0; j <rn; ++ J) if (visy [J]) ly [J] + = D; else slack [J]-= D ;}}// X (-[, b] optimization # define within (X, a, B) (unsigned) (x)-(a) <= (B)-())) // read an integer [returns the same value as scanf] int readint (int * P) {int ch; while (! Within (CH = getchar (), '0', '9') if (CH = EOF) return EOF; int rslt = 0; do rslt = rslt * 10 + (CH-'0'); While (within (CH = getchar (), '0', '9'); * P = rslt; return 1;} int println_int (int I) {char s [107], P = 0; while (I) {s [p ++] = I % 10; i/= 10;} while (p) putchar ('0' + s [-- p]); putchar ('\ n');} int main (void) {# ifndef online_judge freopen ("in.txt", "r", stdin); # endif while (readint (& N)> 0) {for (INT I = 0; I <n; ++ I) // the I-th village for (Int J = 0; j <n; ++ J) readint (& W [I] [J]); Work (); int z = 0; For (Int J = 0; j <rn; ++ J) if (matched [J]> = 0) Z + = W [matched [J] [J]; println_int (z) ;}return 0 ;}
21:44:16 |
Accepted |
2255 |
234 Ms |
656 K |
2298 B |
G ++ |
HDU 2255-big money