# Include <stdio. h> # include <string. h> # define M 310 # define INF 0x3f3f3fint N, NX, NY; int link [m], LX [m], Ly [m], slack [m]; // lx, Ly is the top mark, NX, and NY are the numbers of X point sets y points. Int visx [m], visy [m], W [m] [m]; int DFS (int x) {visx [x] = 1; for (INT y = 1; y <= NY; y ++) {If (visy [y]) continue; int T = Lx [x] + ly [y]-W [x] [Y]; If (t = 0) // {visy [y] = 1; if (link [y] =-1 | DFS (link [y]) {link [y] = x; return 1 ;}} else if (Slack [y]> T) // obtain the minimum slack [y] = T;} return 0;} int km () {int I, j; memset (link,-1, sizeof (Link); memset (ly, 0, sizeof (ly); for (I = 1; I <= NX; I ++) // lx is initialized to the largest for (j = 1, LX [I] =-INF; j <= NY; j ++) associated with it) if (W [I] [J]> lx [I]) lx [I] = W [I] [J]; for (INT x = 1; x <= NX; X ++) {for (I = 1; I <= NY; I ++) slack [I] = inf; while (1) {memset (visx, 0, sizeof (visx); memset (visy, 0, sizeof (visy); If (DFS (x )) // If it succeeds (the augmented orbit is found), The point is extended to enter the augmented break of the next point; // if it fails (the augmented orbit is not found ), you need to change the vertex labels to increase the number of feasible edges in the graph. // The method is to subtract a constant D from all the labels of the X vertices in the augmented orbit (that is, traversed during the augmented process, // Add a constant D int d = inf; for (I = 1; I <= NY; I ++) if (! Visy [I] & D> slack [I]) d = slack [I]; for (I = 1; I <= NX; I ++) if (visx [I]) lx [I]-= D; for (I = 1; I <= NY; I ++) // after the top mark is modified, we need to subtract the slack value of all y vertices not in the staggered tree from D if (visy [I]) ly [I] + = D; else slack [I]-= D ;}} int res = 0; for (I = 1; I <= NY; I ++) if (link [I]>-1) RES + = W [LINK [I] [I]; return res;} int main () {int I, J; while (scanf ("% d", & N )! = EOF) {Nx = ny = N; // memset (W, 0, sizeof (w); for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) scanf ("% d", & W [I] [J]); int ans = km (); printf ("% d \ n", ANS);} return 0 ;}
HDU 2255 Ben Xiaokang makes a lot of money km bare question