HDU 2255 Ben Xiaokang makes a lot of money km bare question

Source: Internet
Author: User


# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.