P3376 [TEMPLATE] maximum network flow (70), p337670

Source: Internet
Author: User

P3376 [TEMPLATE] maximum network flow (70), p337670
Description

For example, give a network diagram and its source and sink points, and find the maximum flow of the network.

Input/Output Format

Input Format:

 

The first line contains four positive integers N, M, S, and T, indicating the number of vertices, the number of directed edges, The Source Vertex sequence number, and the sink vertex sequence number.

Next, each row in line M contains three positive integers: ui, vi, and wi, indicating that the directed edge of line I starts from ui and reaches vi, edge Weight is wi (that is, the maximum traffic of this edge is wi)

 

Output Format:

 

A row contains a positive integer, that is, the maximum stream of the network.

 

Input and Output sample input sample #1:
4 5 4 34 2 304 3 202 3 202 1 301 3 40
Output sample #1:
50
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

For 30% of data: N <= 10, M <= 25

For 70% of data: N <= 200, M <= 1000

For 100% of data: N <= 10000, M <= 100000

Example:

There are three paths in the question:

4 --> 2 --> 3. The route can pass 20 of the traffic.

4 --> 3, with 20 traffic

4 --> 2 --> 1 --> 3, you can use 10 of the traffic (side 4 --> 2 has consumed 20 of the traffic)

Therefore, the total traffic is 20 + 20 + 10 = 50. Output 50.

 

Inexplicably WA three points,

Reschedule in another day

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 # define lli long int 8 using namespace std; 9 const int MAXN = 300001; 10 const int maxn = 0x7ffffff; 11 void read (int & n) 12 {13 char c = '+'; int x = 0; bool flag = 0; 14 while (c <'0' | c> '9') 15 {c = getchar (); if (c = '-') flag = 1;} 16 while (c> = '0' & c <= '9') 17 {x = x * 10 + c-48; c = getchar ();} 18 f Lag = 1? N =-x: n = x; 19} 20 struct node 21 {22 int u, v, flow, cap, nxt; 23} edge [MAXN]; 24 int head [MAXN]; 25 int num = 1; 26 int n, m, S, T; 27 int dis [MAXN]; 28 int vis [MAXN]; 29 int cur [MAXN]; 30 void add_edge (int x, int y, int z) 31 {32 edge [num]. u = x; 33 edge [num]. v = y; 34 edge [num]. cap = z; 35 edge [num]. flow = 0; 36 edge [num]. nxt = head [x]; 37 head [x] = num ++; 38} 39 bool bfs (int bg, int ed) 40 {41 memset (vis, 0, sizeof (Vis); 42 memset (dis, 0, sizeof (dis); 43 queue <int> q; 44 q. push (bg); 45 dis [bg] = 1; 46 vis [bg] = 1; 47 while (! Q. empty () 48 {49 int p = q. front (); 50 q. pop (); 51 for (int I = head [p]; I! =-1; I = edge [I]. nxt) 52 {53 if (! Vis [edge [I]. v] & edge [I]. cap-edge [I]. flow> 0) 54 {55 vis [edge [I]. v] = 1; 56 dis [edge [I]. v] = dis [edge [I]. u] + 1; 57 q. push (edge [I]. v); 58} 59} 60} 61 return vis [ed]; 62} 63 int dfs (int now, int a) //: minimum residue of all arcs 64 {65 if (now = T | a <= 0) 66 return a; 67 int flow = 0, f; 68 for (int I = head [now]; I! =-1; I = edge [I]. nxt) 69 {70 if (dis [now] + 1 = dis [edge [I]. v] & edge [I]. cap-edge [I]. flow> 0) 71 {72 f = dfs (edge [I]. v, min (a, edge [I]. cap-edge [I]. flow); 73 edge [I]. flow + = f; 74 edge [I ^ 1]. flow-= f; 75 flow + = f; 76 a-= f; 77 if (a <= 0) break; 78} 79} 80 return flow; 81} 82 void Dinic (int S, int T) 83 {84 int ansflow = 0; 85 // for (int I = 1; I <= n; I ++) 86 // cur [I] = head [I]; 87 while (bfs (S, T) 88 {89 ansflow + = dfs (S, maxn ); 90} // obtain the level 91 printf ("% d", ansflow); 92 93} 94 int main () 95 {96 read (n); read (m ); 97 // swap (n, m); 98 // S = 1; T = m; 99 read (S); read (T); 100 for (int I = 1; I <= n; I ++) 101 head [I] =-1; 102 for (int I = 1; I <= m; I ++) 103 {104 int x, y, z; 105 read (x); read (y); read (z); 106 add_edge (x, y, z ); 107 add_edge (y, x, 0); 108} 109 Dinic (S, T); 110 return 0; 111}

 

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.