P1343 earthquake and p1343 earthquake
Description
When the Wenchuan earthquake occurred, Sichuan ** middle school was in class. When the earthquake occurred, the teachers immediately led x students to escape. The whole school could be abstracted as a directed graph with n points in the figure, m side. Is the classroom, is the safe area, and each side can only accommodate a certain number of students. If the building is exceeded, the building will collapse because there are too many students, the principal decided to divide the students into several batches for escape. Only after the first batch of students had completely escaped can the second batch of students escape from. Now, please help the principal calculate, the maximum number of students that can be delivered in each batch. x students can only be delivered in batches.
Input/Output Format
Input Format:
3 integers n, m, x (x <2 ^ 31, n <= 200, m <= 2000) in the first row. The following m rows have three integers, b, c (a1, a <> B, 0 describe an edge, which represents an edge from point a to point B and can accommodate c students.
Output Format:
The two integers indicate the maximum number of students that can be delivered in each batch, and the number of x students can be completed in several batches. If the destination cannot be reached (point n), "Orz Ni Jinan Saint Cow!" Is output !"
Input and Output sample input sample #1:
6 7 71 2 11 4 22 3 14 5 14 3 13 6 25 6 1
Output sample #1:
3 3
Description
[Note]
For example
1 2 100
2 3 1
100 students rushed to, and then one student slowly walked along the two sides
18 God's ox stipulates that this is not feasible ......
That is to say, each batch of students must start from the start point and reach the end point at the same time.
To issue a Dinic.
There are few questions. If we want to deliver students, we only need to find the maximum number of students that can be passed each time.
Then, we use a greedy principle to deliver students (nonsense) with the largest delivery solution every time ..)
As long as the maximum stream is not 0, it can be delivered.
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 = 0x7fffff; 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 <1) + (x <3) + c-48; c = getchar ();} 18 flag = 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 = 0; 26 int n, m, S, T, x; 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 (dis,-1, size Of (dis); 42 queue <int> q; 43 q. push (bg); 44 dis [bg] = 0; 45 while (! Q. empty () 46 {47 int p = q. front (); 48 q. pop (); 49 for (int I = head [p]; I! =-1; I = edge [I]. nxt) 50 {51 if (dis [edge [I]. v] =-1 & edge [I]. cap> edge [I]. flow) 52 {53 vis [edge [I]. v] = 1; 54 dis [edge [I]. v] = dis [edge [I]. u] + 1; 55 q. push (edge [I]. v); 56} 57} 58} 59 if (dis [ed] =-1) 60 return 0; 61 else return 1; 62} 63 int dfs (int now, int a) // a: minimum residue of all arcs 64 {65 if (now = T | a <= 0) 66 return a; 67 68 int flow = 0, f; 69 for (int I = head [now]; I! =-1; I = edge [I]. nxt) 70 {71 if (dis [now] + 1 = dis [edge [I]. v] & edge [I]. cap-edge [I]. flow> 0) 72 {73 f = dfs (edge [I]. v, min (a, edge [I]. cap-edge [I]. flow); 74 edge [I]. flow + = f; 75 edge [I ^ 1]. flow-= f; 76 flow + = f; 77 a-= f; 78 if (a <= 0) break; 79} 80} 81 return flow; 82} 83 void Dinic (int S, int T) 84 {85 int ansflow = 0; 86 int cs = 0; 87 int maxflow = 0; 88 for (int I = 1; I <= n; I ++) 89 cur [I] = head [I]; 90 while (bfs (S, T) 9 1 {92 int p = dfs (S, maxn); 93 cs ++; 94 ansflow + = p; 95 maxflow = max (maxflow, p ); 96} // obtain the level 97 if (ansflow = 0) 98 printf ("Orz Ni Jinan Saint Cow! "); 99 else 100 printf (" % d ", ansflow, (x % ansflow) = 0? (X/ansflow) :( x/ansflow + 1); 101 102 103 // printf ("% d", ansflow); 104 105} int main () 106 {107 scanf ("% d", & n, & m, & x); 108 // read (n); read (m ); read (x); 109 // swap (n, m); 110 S = 1; T = n; 111 // read (S); read (T ); 112 for (int I = 1; I <= n; I ++) 113 head [I] =-1; 114 for (int I = 1; I <= m; I ++) 115 {116 int x, y, z; 117 // read (x); read (y); read (z ); 118 scanf ("% d", & x, & y, & z); 119 add_edge (x, y, z); 120 add_edge (y, x, x, 0); 121} 122 Dinic (S, T); 123 return 0; 124}