Poj 2135 farm tour (maximum minimum cost Stream)

Source: Internet
Author: User

FJ takes his friends to visit the farm. Their starting point is 1 and the ending point is N. There may be paths between points. It is now required to take the shortest path from 1 to n and return from N to 1. In addition, the road that has been taken cannot be taken again when returning!
Problem: The minimum fee flow. Each route is taken only once, indicating that the capacity of each edge is 1. Because it is an undirected graph, the process of going back from N to 1 is equivalent to going from 1 to n again. Then we need to find two shortest paths from 1 to n. Take a super source and connect it to 1. The edge capacity is 2 (two paths) and the cost is 0. N is connected to a superlevel sink. The capacity is also 2 and the fee is 0. Pay special attention to Edge Addition !!!

For example, the figure below:

If the input is such a set of data:
4 5
1 2 10
1 3 2
2 4 4
2 3 1
3 4 10
When only one side is added. The augmented path of the first question will be 1-> 3-> 4. Instead of 1-> 3-> 2-> 4

# Include <iostream> using namespace STD; # define Max 30000 # define INF 999999999 # define min (a, B) (a <B? A: B) struct edge {int St, Ed; int cost, flow; int next;} edge [Max * 2]; int dis [Max]; int pre [Max]; int head [Max]; int que [Max]; bool mark [Max]; int e, SRC, DEST; void add_edge (int u, int V, int FLW, int Cst) {edge [e]. st = u; edge [e]. ed = V; edge [e]. flow = FLW; edge [e]. cost = CST; edge [e]. next = head [u]; head [u] = e ++; edge [e]. st = V; edge [e]. ed = u; edge [e]. flow = 0;/* the residual network is 0 at the beginning, so the reverse side capacity is 0. (It is only used to represent the residual network !) * // * Think about it. The figure is an undirected graph and the capacity from u to V is FLW. Naturally, the initial capacity from V to u should also be FLW !. Therefore, you also need to create an edge (v, u ). */Edge [e]. cost =-CST; edge [e]. next = head [v]; head [v] = e ++;} void spfa () {int U, V, I, front, rear; memset (mark, 0, sizeof (Mark); memset (PRE,-1, sizeof (pre); for (I = SRC; I <= DEST; I ++) dis [I] = inf; front = rear = 0; que [rear ++] = SRC; DIS [SRC] = 0; Mark [SRC] = true; while (front! = (Rear + 1) % max) {u = que [Front]; front = (front + 1) % Max; Mark [u] = false; for (I = head [u]; I! =-1; I = edge [I]. next) {v = edge [I]. ed; If (edge [I]. flow> 0 & dis [v]> dis [u] + edge [I]. cost) {dis [v] = dis [u] + edge [I]. cost; Pre [v] = I; If (! Mark [v]) {mark [v] = true; que [rear] = V; Rear = (Rear + 1) % Max ;}}} int mcmf () {int res = 0; while (1) {spfa (); If (DIS [DEST] = inf) break; int u = DEST, minf = inf; while (u! = SRC) {minf = min (edge [pre [u]. flow, minf); U = edge [pre [u]. st;} u = DEST; while (u! = SRC) {edge [pre [u]. flow-= minf; edge [pre [u] ^ 1]. flow + = minf; Res + = edge [pre [u]. cost * minf; u = edge [pre [u]. st ;}} return res;} int main () {int U, V, Len, n, m; scanf ("% d", & N, & M ); E = src = 0; DEST = n + 1; memset (Head,-1, sizeof (head); While (M --) {scanf ("% d", & U, & V, & Len); add_edge (u, v, 1, Len); add_edge (v, U, 1, Len); // It's stuck here ···.} Add_edge (SRC, 1, 2, 0); add_edge (n, DEST, 2, 0); printf ("% d \ n", mcmf ()); system ("pause"); Return 0 ;}

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.