Billing flow: Number of squares

Source: Internet
Author: User

It has been a long time to struggle with this problem. It is not difficult to solve the problem. Simply use DP. However, it can also be a classic example of network stream modeling. Today, I finally transferred the noip2008 text. By the wayProgramChanged the number of squares in noip2000, 3, and N.

TheAlgorithmSee noip2008 document transfer fee stream Modeling

It turns out that only the input is different, but the number of passing notes is different from that of the square. Therefore, each student is required to only help once, so you can split the notes and connect them to one side, the number of squares can be repeated (although the number has been taken away), so you have to connect an edge with a cost of 0 and a capacity of INF to repeat it after selection. I didn't take it into consideration at the beginning. Wa has been used for many times.

For how to get the number of squares, you can refer to getting the number of squares from 2 to n to get the number of squares and N to get the number of squares.CodeDisgusting (well, I admit that my code is ugly too)

3. Take the number of squares and n take the number of squares on tyvj, p1413 and p1414. The original question of noip is the number of passing notes and squares, which is everywhere. In addition, there is an adaptation of the number of squares on the HDU. If you have time, just remove it.

I found that not often practiced is really not good. After the College Entrance Examination holiday, I haven't written a fee stream for more than ten days. Now it takes a long time to write a bare fee stream. I still copied my original code, and a bunch of bugs ......

In addition, to write such code that contains more than one hundred lines, we need to standardize it. My code is still ugly. For example, queue operations can be placed in the process.

The attachment notes are slightly different from the N checkpoint source program, except that the add_edge and init processes are slightly different.

N number of squares:

 Const OO = 19930317; var E, C, next, OPP, Q, cost: array [0 .. 100000] of longint; Dist, G, pre, pre_edge: array [0 .. 100000] of longint; X: array [0 .. 100, 0 .. 100] of longint; flag: array [0 .. 100000] of Boolean; I, J, K, A, B: longint; max_cost: longint; S, T: longint; n, m: longint; Size: longint; now, flow, P: longint; function min (A, B: longint): longint; begin If A <B then exit () Else Exit (B); end; Procedure add_edge (X, Y, Z, W: longint); begin Inc (size); e [size]: = y; C [size]: = W; next [size]: = G [X]; G [x]: = size; cost [size]: = z; OPP [size]: = size + 1; INC (size); e [size]: = x; C [size]: = 0; next [size]: = G [y]; G [y]: = size; cost [size]: =-Z; OPP [size]: = size-1; end; function spfa: Boolean; var head, tail, I, j, P: longint; begin head: = 0; tail: = 1; Q [1]: = s; For I: = 1 to T Do Dist [I]: =-Oo; Dist [s]: = 0; fillchar (flag,Sizeof (FLAG), 0); flag [s]: = True ; While Head <tail Do Begin Inc (head); I: = Q [head]; flag [I]: = False ; P: = G [I]; While P <> 0 Do Begin J: = E [p]; If (Dist [I] + cost [p]> Dist [J]) and (C [p]> 0) then begin Dist [J]: = cost [p] + dist [I]; Pre [J]: = I; pre_edge [J]: = P; If Not flag [J] Then begin Inc (tail); Q [tail]: = J; flag [J]: = True ; End; P: = next [p]; end; exit (Dist [T] <>-oo); end; Procedure Init; begin readln (K, m, n); j: = 1; For I: = 1 to n Do      For J: = 1 to m Do Read (X [I, j]); For I: = 1 to n Do      For J: = 1 to m Do Begin a: = m * (I-1) + J; B: = A * 2; A: = B-1; add_edge (a, B, X [I, j], 1 ); add_edge (a, B, 0, OO); end; For I: = 1 to n Do Begin For J: = 1 to mDo Begin a: = (M * (I-1) + J) * 2; If J <m then add_edge (A, (I-1) * m + J + 1) * 2-, OO ); If I <n then add_edge (A, (I * m) + J) * 2-1, 0, OO); end; s: = N * m * 2 + 1; t: = S + 1; add_edge (S, 1,0, OO); add_edge (N * m * 2, T, 0, k); end; Procedure main; begin While Spfa Do Begin now: = T; flow: = Oo; While Now <> S Do Begin flow: = min (flow, C [pre_edge [now]); now: = pre [now]; end; now: = T; While Now <> S Do Begin P: = pre_edge [now]; Dec (C [p], flow); Inc (C [OPP [p], flow); Inc (max_cost, cost [p] * flow); now: = pre [now]; end; begin Init; main; writeln (max_cost); end.

Text:

 Const OO = 19930317; var E, C, next, OPP, Q, cost: array [0 .. 100000] of longint; Dist, G, pre, pre_edge: array [0 .. 100000] of longint; flag: array [0 .. 100000] of Boolean; I, J, K, a, B, X: longint; max_cost: longint; S, T: longint; n, m: longint; Size: longint; now, flow, P: longint; function min (A, B: longint): longint; begin If A <B then exit () Else Exit (B); end; Procedure add_edge (x, y, z: longint); begin Inc (size); e [size]: = y; C [size]: = 1; next [size]: = G [X]; G [x]: = size; cost [size]: = z; OPP [size]: = size + 1; INC (size); e [size]: = x; C [size]: = 0; next [size]: = G [y]; G [y]: = size; cost [size]: =-Z; OPP [size]: = size-1; end; function spfa: Boolean; var head, tail, I, j, P: longint; begin head: = 0; tail: = 1; Q [1]: = s; For I: = 1 to T Do Dist [I]: =-Oo; Dist [s]: = 0; fillchar (flag, Sizeof (FLAG), 0); flag [s]: = True ; While Head <tail Do Begin Inc (head); I: = Q [head]; flag [I]: = False ; P: = G [I]; While P <> 0Do Begin J: = E [p]; If (Dist [I] + cost [p]> Dist [J]) and (C [p]> 0) then begin Dist [J]: = cost [p] + dist [I]; Pre [J]: = I; pre_edge [J]: = P; If Not flag [J] Then begin Inc (tail); Q [tail]: = J; flag [J]: = True ; End; P: = next [p]; end; exit (Dist [T] <>-oo); end; Procedure Init; begin readln (n, m ); For I: = 1 to n Do      For J: = 1 to m Do Begin read (x); A: = m * (I-1) + J; B: = A * 2; A: = B-1; add_edge (a, B, x); end; c [1]: = 2; C [size-1]: = 2;For I: = 1 to n Do Begin For J: = 1 to m Do Begin a: = (M * (I-1) + J) * 2; If J <m then add_edge (A, (I-1) * m + J + 1) * 2 ); If I <n then add_edge (A, (I * m) + J) * 2-1, 0); end; s: = 1; t: = N * m * 2; end; Procedure main; begin While Spfa Do Begin now: = T; flow: = Oo; While Now <> S Do Begin flow: = min (flow, C [pre_edge [now]); now: = pre [now]; end; now: = T; While Now <> S Do Begin P: = pre_edge [now]; Dec (C [p], flow); Inc (C [OPP [p], flow); Inc (max_cost, cost [p] * flow); now: = pre [now]; end; begin Init; main; writeln (max_cost); end.

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.