Poj 2135 farm tour (minimum fee Stream)

Source: Internet
Author: User

// Minimum fee stream <br/> // The Key to the diagram is to add a super source and a super collection point <br/> // you need to find a way to make a round trip, the sum of the two paths is the shortest and each path can only go through one <br/> // The first step is to find two paths from the start point to the end point <br/> // The second links to the billing flow, the distance is used as the cost. The capacity is set to 1 to ensure that each route passes only once <br/> // The capacity of the Super source point and the start point is 2, the capacity of the destination vertex and super vertex is 2, so that a maximum of 2 routes can be found <br/> // The final mincost is the answer <br/> // pay attention to the bidirectional edge condition, when two or more sides exist, <br/> # include <iostream> <br/> # include <br/> <queue> <br/> using namespace STD; <br/> const int maxn = 1003; <br/> const int maxm = 40005; <br/> const int INF = 1 000000000; <br/> int mincost, maxflow; <br/> int n, m; <br/> int U [maxm], V [maxm], Cap [maxm], flow [maxm], cost [maxm], next [maxm]; <br/> int head [maxn], pre [maxn], edge [maxn], DIS [maxn]; <br/> int m; <br/> void addedge (int u, int V, int cap, int cost) <br/> {<br/> flow [m] = flow [M + 1] = 0; <br/> V [m] = V; <br/> U [m] = u; <br/> CAP [m] = CAP; <br/> cost [m] = cost; <br/> next [m] = head [u]; <br/> head [u] = m ++; </P> <p> V [m] = u; <B R/> U [m] = V; <br/> CAP [m] = 0; <br/> cost [m] =-cost; <br/> next [m] = head [v]; <br/> head [v] = m ++; <br/>}< br/> void buildgraph () <br/>{< br/> scanf ("% d", & N, & M); <br/> int U, V, C; <br/> m = 0; <br/> memset (Head,-1, sizeof (head); <br/> while (M --) <br/>{< br/> scanf ("% d", & U, & V, & C); <br/> addedge (u, v, 1, C); <br/> addedge (v, U, 1, C); <br/>}< br/> addedge (0, 1, 2, 0 ); <br/> addedge (n, n + 1, 2, 0); <br/>}< br/> void mcmf (Int St, int ed) <br/>{< br/> queue <int> q; <br/> memset (flow, 0, sizeof (flow )); <br/> mincost = maxflow = 0; <br/> for (;) <br/> {<br/> bool INQ [maxn]; <br/> for (INT I = sT; I <= Ed; ++ I) dis [I] = (I = sT? 0: INF); <br/> memset (INQ, 0, sizeof (INQ); <br/> q. Push (ST); <br/> while (! Q. empty () <br/>{< br/> int u = Q. front (); q. pop (); <br/> INQ [u] = 0; <br/> for (int e = head [u]; e! =-1; E = next [e]) <br/>{< br/> If (Cap [e]> flow [e] & dis [u] + cost [e] <dis [V [e]) <br/> {<br/> dis [V [e] = dis [u] + cost [E]; <br/> pre [V [e] = U [E]; <br/> edge [V [e] = E; <br/> If (! INQ [V [e]) <br/>{< br/> q. push (V [e]); <br/> INQ [V [e] = 1; <br/>}< br/>}// spfa extended <br/> If (DIS [ed] = inf) break; <br/> int Delta = inf; // Delta can be improved <br/> for (INT u = Ed; u! = ST; u = pre [u]) <br/> Delta = min (delta, Cap [edge [u]-flow [edge [u]); // traverse the edges of the shortest path and modify the number of improvements <br/> for (INT u = Ed; u! = ST; u = pre [u]) <br/>{< br/> flow [edge [u] + = delta; // update forward traffic <br/> flow [edge [u] ^ 1]-= delta; // obtain the reverse edge number through an exception or 1, and updates reverse traffic <br/>}< br/> mincost + = dis [ed] * delta; <br/> maxflow + = delta; <br/>}</P> <p> int main () <br/>{< br/> // freopen ("in.txt ", "r", stdin); <br/> buildgraph (); <br/> mcmf (0, n + 1 ); <br/> printf ("% d/N", mincost); <br/> return 0; <br/>} 

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.