HDU 1532 Drainage Ditches (maximum stream template ISAP)

Source: Internet
Author: User

HDU 1532 Drainage Ditches (maximum stream template ISAP)
Drainage DitchesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 11306 Accepted Submission (s): 5328



Problem Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. this means that the clover is covered by water for awhile and takes quite a long time to regrow. thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. instead, the water is drained to a nearby stream. being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. for any given ditch, water flows in only one ction, but there might be a way that water can flow in a circle.

Input The input parameter des several cases. for each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200 ). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. intersection 1 is the pond. intersection point M is the stream. each of the following N lines contains three integers, Si, Ei, and Ci. si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. water will flow through this ditch from Si to Ei. ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 41 2 401 4 202 4 202 3 303 4 10

Sample Output
50

Source USACO 93

The number of edges N and the number of points M are given. Each edge is unidirectional. Ask the maximum number of streams from 1 to M.

Method 1: ISAP

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define captype _ int64const int MAXN = 100010; const int MAXM = 400010; const int INF = 1 <30; struct EDG {int to, next; captype cap, flow;} edg [MAXM]; int eid, head [MAXN]; int gap [MAXN]; // each distance (or can be considered as a height) number of vertices int dis [MAXN]; // The shortest distance between each vertex and eNode int cur [MAXN]; // cur [u] indicates that starting from the u point, it can flow through the cur [u] side void init () {eid = 0; memset (head,-1, sizeof (head);} // three parameters with a directed edge and four parameters without a directed edge void addEdg (int u, int v, captype c, captype rc = 0) {edg [eid]. to = v; edg [eid]. next = head [u]; edg [eid]. cap = c; edg [eid]. flow = 0; head [u] = eid ++; edg [eid]. to = u; edg [eid]. next = head [v]; edg [eid]. cap = rc; edg [eid]. flow = 0; head [v] = eid ++;} // pre-processes the shortest void BFS (int sNode, int eNode) to all vertices {queue
    
     
Q; memset (gap, 0, sizeof (gap); memset (dis,-1, sizeof (dis); gap [0] = 1; dis [eNode] = 0; q. push (eNode); while (! Q. empty () {int u = q. front (); q. pop (); for (int I = head [u]; I! =-1; I = edg [I]. next) {int v = edg [I]. to; if (dis [v] =-1) {dis [v] = dis [u] + 1; gap [dis [v] ++; q. push (v) ;}}} int S [MAXN]; // path stack, storing the ID number of the edge captype maxFlow_sap (int sNode, int eNode, int n) {BFS (sNode, eNode); // pre-processes the shortest distance from eNode to all vertices. if (dis [sNode] =-1) return 0; // memcpy (cur, head, sizeof (head) from the source node to the inaccessible vertex; int top = 0; // captype ans = 0 on the top of the stack; // maximum stream int u = sNode; while (dis [sNode]
     
      
Edg [S [I]. cap-edg [S [I]. flow) {Min = edg [S [I]. cap-edg [S [I]. flow; inser = I;} for (int I = 0; I
      
        0 & dis [u] = dis [v] + 1) {flag = true; cur [u] = I; break ;}} if (flag) {S [top ++] = cur [u]; // Add an edge u = v; continue;} // if no adjacent vertex that can flow is found, then, the distance (or height) of the starting point u is changed to the minimum distance of the adjacent streamable point + 1 int Mind = n; for (int I = head [u]; I! =-1; I = edg [I]. next) {if (edg [I]. cap-edg [I]. flow> 0 & Mind> dis [edg [I]. to]) {Mind = dis [edg [I]. to]; cur [u] = I;} gap [dis [u] --; if (gap [dis [u] = 0) return ans; // when the distance of dis [u] is absent, it is impossible to find an augmented stream path from the source point. // there is only one distance between the sink point and the current point, then, from the source point to the sink point will inevitably pass through the current point. However, if the current point fails to find the point that can flow, it will inevitably cut off dis [u] = Mind + 1; // If a adjacent vertex that can be streamed is found, the distance between the adjacent vertex is + 1. If not, the distance is n + 1 gap [dis [u] ++; if (u! = SNode) u = edg [S [-- top] ^ 1]. to; // return an edge} return ans;} int main () {int n, m, u, v; captype c; while (scanf ("% d ", & m, & n)> 0) {init (); while (m --) {scanf ("% d % I64d", & u, & v, & c); addEdg (u, v, c);} printf ("% I64d \ n", maxFlow_sap (1, n, n ));}}
      
     
    
   
  
 
Method 2: Press and re-mark push_relabel

 

 

# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define captype _ int64const int N = 210; const int MAX = 1 <30; struct EDG {int to, nxt; captype c; // residue of each edge} edg [N * N]; int head [N], eid; captype vf [N]; // The remaining traffic of the vertex int h [N]; // The height of the vertex // int n; // The total number of vertex numbers, including the Source Vertex and sink vertex int min (int, int B) {return a> B? B: a;} void init () {memset (head,-1, sizeof (head); eid = 0;} // Add a directed edge void addEdg (int u, int v, captype c) {edg [eid]. to = v; edg [eid]. nxt = head [u]; edg [eid]. c = c; head [u] = eid ++; edg [eid]. to = u; edg [eid]. nxt = head [v]; edg [eid]. c = 0; head [v] = eid ++;} captype maxFlow (int sNode, int eNode, int n) {// Source and Sink captype minh, ans = 0; queue
    
     
Q; memset (h, 0, sizeof (h); memset (vf, 0, sizeof (vf); h [sNode] = n + 1; // vf [sNode] = MAX; // The height of the Source Vertex. push (sNode); while (! Q. empty () {int u = q. front (); q. pop (); minh = MAX; for (int I = head [u]; I! =-1; I = edg [I]. nxt) {int v = edg [I]. to; captype fp; if (edg [I]. c
     
      
0) {minh = min (minh, h [v]); if (u = sNode | h [u] = h [v] + 1) {edg [I]. c-= fp; edg [I ^ 1]. c + = fp; // returns the reverse edge to the reverse channel vf [u]-= fp; vf [v] + = fp; if (v = eNode) ans + = fp; // if (v! = SNode & v! = ENode) // queue q. push (v) ;}}if (vf [u] = 0) break; // if the remainder of the vertex is 0, then you can jump out of the for Loop} // if it is not the source point (or not the sink point), and the vertex still has a stream, then re-mark the height + 1 into the queue // here the value is the highest of the adjacent vertex height of a unit, you can also simply in the original height + 1 if (u! = SNode & vf [u]> 0) {h [u] = minh + 1; q. push (u) ;}} return ans ;}int main () {int n, m, u, v; captype c; while (scanf ("% d ", & m, & n)> 0) {init (); while (m --) {scanf ("% d % I64d", & u, & v, & c); addEdg (u, v, c);} printf ("% I64d \ n", maxFlow (1, n, n ));}}
     
    
   
  
 
Method 3: EK
# Include
 
  
# Include
  
   
# Include
   
    
Using namespace std; # define captype _ int64const int N = 205; captype cap [N] [N], f [N] [N], rest [N]; int sNode, eNode, pre [N]; void init () {memset (f, 0, sizeof (f); memset (cap, 0, sizeof (cap ));} bool searchPath (int n) {// find an augmented path bool vist [N] = {0}; queue
    
     
Q; int u, v; u = sNode; vist [u] = 1; pre [u] = u; rest [u] = 1 <30; q. push (u); while (! Q. empty () {u = q. front (); q. pop (); for (v = 1; v <= n; v ++) if (! Vist [v] & cap [u] [v]-f [u] [v]> 0) {vist [v] = 1; pre [v] = u; if (cap [u] [v]-f [u] [v]> rest [u]) rest [v] = rest [u]; else rest [v] = cap [u] [v]-f [u] [v]; if (v = eNode) return true; q. push (v) ;}} return false;} captype maxflow (int s, int t, int n) {captype ans = 0; sNode = s; eNode = t; while (searchPath (n) {ans + = rest [eNode]; int v = eNode; while (v! = SNode) {int u = pre [v]; f [u] [v] + = rest [eNode]; f [v] [u]-= rest [eNode]; // give a backflow opportunity v = u;} return ans;} int main () {int n, m, u, v; captype c; while (scanf ("% d", & m, & n)> 0) {init (); while (m --) {scanf ("% d % I64d", & u, & v, & c); cap [u] [v] + = c ;} printf ("% I64d \ n", maxflow (1, n, n ));}}
    
   
  
 


 


 

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.