Billing flow: getting started

Source: Internet
Author: User

I recently looked at the billing flow to find a suitable imitationProgramLooking at the materials, I think it is simpler than the biggest stream for beginners (of course, the level is different from that of four months ago). The basic idea is to pay for the edge and use the shortest circuit.AlgorithmFind the path for increasing the minimum cost. The main program is a bit similar to the ek algorithm of the largest stream, and it uses spfa to find the minimum cost and add light to the base. It is also a very familiar algorithm. After imitating one program, I typed one program myself. The second program was completed in 12 minutes. However, after debugging for a long time, both programs encountered variable errors or a low-level error of missing statements ......

If you have time, you need to study the best Binary Graph Matching, zkw, and other algorithms. Familiarize yourself with modeling, linear programming of parameters, and 24 questions about network flow. Then, find some classic questions, the basic network flow should not be a problem.

With the minimum fee flow program:

 Const OO = 19930508; var Dist, path: array [0 .. 1000] of longint; F, cost: array [0 .. 1000, 0 .. 1000] of longint; I, J, K, M, N, X: longint; maxflow, mincost, flow: longint; Q: array [0 .. 100000] of longint; flag: array [0 .. 10000] of Boolean; Head, tail: longint; now, pre: longint; S, T: longint; y, z, W: longint; function min (A, B: longint): longint; begin If A <B then exit () Else Exit (B); end; function spfa: Boolean; begin fillchar (flag,Sizeof (FLAG), 0 ); For I: = 1 to n Do Dist [I]: = Oo; head: = 0; tail: = 1; Q [1]: = s; flag [s]: = True ; Dist [s]: = 0; While Head <tail Do Begin Inc (head); I: = Q [head]; flag [I]: = False ; For J: = 1 to n Do        If (F [I, j]> 0) and (Dist [J]> Dist [I] + cost [I, j]) then begin Dist [J]: = DIST [I] + cost [I, j]; path [J]: = I; If Not flag [J] Then begin Inc (tail); Q [tail]: = J; flag [J]: = True ; End; exit (Dist [T] <> OO); end; Procedure Init; begin readln (n, m ); For I: = 1 to n Do Begin For J: = 1 to n Do Cost [I, j]: = Oo; cost [I, I]: = 0; end; For I: = 1 to m Do Begin readln (X, Y, Z, W); F [x, y]: = z; cost [x, y]: = W; end; s: = 1; t: = N; end; Procedure main; begin While Spfa Do Begin now: = T; flow: = Oo; While Now <> S Do Begin pre: = path [now]; flow: = min (flow, F [pre, now]); now: = pre; end; Inc (maxflow, flow); now: = T; While Now <> S Do Begin pre: = path [now]; Dec (F [pre, now], flow); Inc (F [now, pre], flow); cost [now, pre]: =-cost [pre, now]; mincost: = mincost + cost [pre, now] * flow; now: = pre; end; Procedure print; begin writeln (mincost); end; begin assign (input, 'Data. in' ); Reset (input); assign (output, 'Data. out' ); Rewrite (output); Init; main; print; close (input); close (output); 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.