Maximum stream: Basic

Source: Internet
Author: User

For the first time in a few monthsCodeI don't know how to write the largest naked stream: rqnoj 194: Student transportation,AlgorithmThere are too many definitions, analyses, and proofs in the introduction. I didn't quite understand them at the beginning. In contrast, data structures and algorithm analysis have diagrams, but no code is provided.

Later, I learned that the BFS augmented algorithm I wrote was called the Edmonds-Karp Algorithm. It belongs to the Ford-Fulkerson method and is more efficient than the DFS augmented algorithm. However, it can only deal with weak data on RQ, I still cannot meet strong questions.

Recently, I was excited to learn about the network stream. I want to learn about dinic. I have understood some of the vague Courseware on Wang Xin, but I have not found any code suitable for imitation.

Then we will find a complete set of network stream materials, including not only dinic analysis, but also sap courseware and code. Then we can see zkw's sap algorithm experiences: "In fact, SAP algorithms are easier to understand, time efficiency is higher, programming is simpler, and ideas are clearer .", The ditch Code provided by using SAP for usaco is shorter than that of my Ek, And then I betrayed dinic and transferred to sap.

SAP has current arc and gap optimization, and has not tried to use it as the front arc.

In the morning, I made noi2006 the biggest profit, with clever ideas. The Dark Book p317 had an analysis of almost the same question. I have no ability to think about the largest closed-Right subgraphs independently. Let's take a look.

My rqnoj194 code:

VaR A: array [1 .. 1000, 1 .. 1000] of longint; q, Prev, low: array [1 .. 100000] of longint; m, n, Max: longint; Procedure Init; var I, j, x, y, z: longint; begin readln (m, n ); For I: = 1 to m Do Begin read (x, y, z); A [x, y]: = z; end; Procedure max_flow; var I, J, K, X, Head, tail, s, T: longint; begin While   True   Do Begin fillchar (low, Sizeof (Low), 0 ); // Fillchar (prev, sizeof (prev), 0); I don't konw why can't = 0 and change (prev [I] <0) to <> 0?      For I: = 1 to n Do Prev [I]: =-1; head: = 1; tail: = 1; Q [head]: = 1; s: = 1; T: = N; low [1]: = maxint; While Head <= tail Do Begin X: = Q [head]; Inc (head ); For I: = 1 to nDo          If (A [x, I]> 0) and (prev [I] <0) then begin Inc (tail); Q [tail]: = I; If Low [x] <A [x, I] Then low [I]: = low [x] Else Low [I]: = A [x, I]; Prev [I]: = x; end; If Prev [T]> 0 then Break ; End; If Prev [T]> 0 then begin X: = T; Inc (max, low [T]); While X <> S Do Begin a [x, Prev [x]: = A [x, Prev [x] + low [T]; A [Prev [X], X]: = A [Prev [X], X]-low [T]; X: = Prev [X]; end; End Else  Break ; End; Procedure print; begin writeln (max); end; // ================= Main ======================== Begin Init; max_flow; print; end.

My maximum profit code (80 points)

 Const OO = 19930508;//? VaR VL, H, G, benefit, cost, E, C, OPP, next: array [0 .. 400000] of longint; augc, S, T, I, J, K, M, N: longint; flow, TMP, size, X, Y, Z, TOT: longint; found: Boolean; Procedure addedge (x, y, z: longint); begin Inc (size); e [size]: = y; next [size]: = G [X]; G [x]: = size; OPP [size]: = size + 1; C [size]: = z; Inc (size ); E [size]: = x; next [size]: = G [y]; G [y]: = size; OPP [size]: = size-1; c [size]: = 0; end; Procedure Init; begin readln (n, m ); // M: User N: Station    For I: = 1 to n Do Read (cost [I]); For I: = 1 to m Do Begin readln (X, Y, benefit [I]); addedge (I, m + X, OO); addedge (I, m + Y, OO); end; s: = 0; // S: 0 T: N + m + 1 1 .. M: User m + 1 .. m + n: Station T: = m + n + 1; Tot: = t + 1;For I: = 1 to n Do Addedge (m + I, T, cost [I]); For I: = 1 to m Do Addedge (S, I, benefit [I]); end; Procedure SAP (M: longint); var Minh, P, augco, I: longint; begin Minh: = tot-1; augco: = augc; // Backup Now augc    If M = t then begin found: = True ; Inc (flow, augc); exit; // Found End; P: = G [m]; While P> 0 Do Begin I: = E [p]; If C [p]> 0 then begin If H [I] + 1 = H [m] Then // Can Begin If C [p] <augc then augc: = C [p]; SAP (I ); // DFS          If H [s]> = tot then exit; // No way          If Found then Break ; // Found Augc: = augco; // Recover the augc End; If H [I] <Minh then Minh: = H [I]; // Adjustment End; P: = next [p]; end; If Not found then begin Dec (FLAC [H [m]);// Gap      If Vl [H [m] = 0 Then H [s]: = tot; H [m]: = Minh + 1; Inc (FLAC [H [m]); End Else Begin Dec (C [p], augc); Inc (C [OPP [p], augc); end; Procedure main; begin FLAC [0]: = tot; While H [0] <tot Do Begin augc: = Oo; found: = False ; SAP (0); end; Procedure print; begin For I: = 1 to m Do INC (TMP, benefit [I]); writeln (TMP-flow); end; begin assign (input, 'Fit. in' ); Reset (input); assign (output, 'Fit. out' ); Rewrite (output); Init; main; print; close (output); close (input); end.

Because it imitates others, it is hard to understand why the total number of nodes is t + 1. In fact, the source point is set to 0. The augmented process involves less than one local variable, and debugging takes more than an hour. I am wondering why this program can go through all the points (but his data range seems to be too small ).ProgramThe difference is: I use a found function to record whether the augmented path is found, and it is based on whether the returned augmented Path Traffic is 0. According to his program, I changed it to AC:

 Const OO = 19930508; //? VaR VL, H, G, benefit, cost, E, C, OPP, next: array [0 .. 400000] of longint; augc, S, T, I, J, K, M, N: longint; flow, TMP, size, X, Y, Z, TOT: longint; found: Boolean; Procedure addedge (x, y, z: longint); begin Inc (size); e [size]: = y; next [size]: = G [X]; G [x]: = size; OPP [size]: = size + 1; C [size]: = z; Inc (size ); E [size]: = x; next [size]: = G [y]; G [y]: = size; OPP [size]: = size-1; c [size]: = 0; end; Procedure Init; begin readln (n, m ); // M: User N: Station    For I: = 1 to n Do Read (cost [I]); For I: = 1 to m Do Begin readln (X, Y, benefit [I]); addedge (I, m + X, OO); addedge (I, m + Y, OO); end; s: = 0; // S: 0 T: N + m + 1 1 .. M: User m + 1 .. m + n: Station T: = m + n + 1; Tot: = t + 1; For I: = 1 to n Do Addedge (m + I, T, cost [I]); For I: = 1 to m Do Addedge (S, I, benefit [I]); end; function SAP (I, now: longint): longint; var P, Minh, TMP, J: longint; begin Minh: = tot-1; SAP: = 0; If I = t then begin Inc (flow, now); exit (now); end; P: = G [I]; While P> 0 Do Begin J: = E [p]; If C [p]> 0 then begin If H [J] + 1 = H [I] Then begin If C [p] <now then TMP: = SAP (J, C [p])Else TMP: = SAP (J, now); Dec (now, TMP); Inc (SAP, TMP); Dec (C [p], TMP ); INC (C [OPP [p], TMP ); If H [s]> = tot then exit; If Now = 0 then Break ; End; If H [J] <Minh then Minh: = H [J]; end; P: = next [p]; end; If SAP = 0 then begin Dec (successively [H [I]); If * H [H [I] = 0 Then H [s]: = tot; H [I]: = Minh + 1; Inc (* H [I]); end; end; Procedure work; begin FLAC [0]: = tot; While H [s] <tot Do SAP (S, OO); end; Procedure print; beginFor I: = 1 to m Do INC (TMP, benefit [I]); writeln (TMP-flow); end; begin assign (input, 'Fit. in' ); Reset (input); assign (output, 'Fit. out' ); Rewrite (output); Init; work; print; close (output); close (input); 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.