[Cpp]
# Include <set>
# Include <map>
# Include <list>
# Include <cmath>
# Include <ctime>
# Include <deque>
# Include <queue>
# Include <stack>
# Include <cctype>
# Include <cstdio>
# Include <string>
# Include <vector>
# Include <cassert>
# Include <cstdlib>
# Include <cstring>
# Include <sstream>
# Include <iostream>
# Include <algorithm>
Using namespace std;
Int max (int a, int B) {return a <B? B: ;}
Int min (int a, int B) {return a> B? B: ;}
# Define V 205
# Define INF 0x3f3f3f
Int cap [V] [V], flow [V] [V]; // cap indicates the capacity of each edge, and flow indicates the traffic of each edge.
Int res [V], father [V]; // res [] indicates the remaining traffic of each vertex. father [] is the father of each vertex.
Int s, t, u, v, f = 0; // f is the largest stream.
Int m, n, a, B, c;
Int EK (int s, int t) // template of the White Book EK, which is easy to understand by adding some notes
{
F = 0;
Queue <int> q;
Memset (flow, 0, sizeof (flow); // The traffic on each edge at the beginning is 0
While (1)
{
Memset (res, 0, sizeof (res); // The residual traffic has to be changed to 0. At first, none of the points have flowed in, right?
Res [s] = INF; // source point. It is necessary to have unlimited traffic...
Q. push (s); // start from the source point for BFS to find the augmented path
While (! Q. empty ())
{
U = q. front (); // gets the first line
Q. pop ();
For (v = 1; v <= n; v ++) // traverses all vertices and finds feasible edges.
{
If (! Res [v] & cap [u] [v]> flow [u] [v]) // The remaining traffic at this point is 0 and the capacity is greater than the traffic, that is, a new node is found.
{
Father [v] = u; // find the new node. Record the parent node.
Q. push (v); // The new node must be in the queue.
Res [v] = min (res [u], cap [u] [v]-flow [u] [v]); // if the remaining traffic of u can be filled with uv, it will be filled with uv. If not, all the traffic of u will flow to uv.
}
}
}
If (res [t] = 0) // if the current stream is already the largest, there is no residual traffic in the sink.
Return f;
For (u = t; u! = S; u = father [u]) // if it can be extended, trace back and update the traffic of each edge from the sink.
{
Flow [father [u] [u] + = res [t]; // update forward traffic
Flow [u] [father [u]-= res [t]; // updates reverse traffic
} Www.2cto.com
F + = res [t]; // find the augmented traffic.
}
}
Int main ()
{
While (cin> m> n) // contributes to WA once.
{
Memset (cap, 0, sizeof (cap ));
Memset (flow, 0, sizeof (flow ));
Memset (father, 0, sizeof (father ));
Memset (res, 0, sizeof (res ));
While (m --)
{
Cin> a> B> c;
Cap [a] [B] + = c; // Add a WAF
}
Cout <EK (1, n) <endl;
}
Return 0;
}
EK with the most comments ever...