We know that the time responsibility of the augmented circuit EK algorithm is O (n * m ^ 2), and the time complexity of finding the shortest augmented path is O (M * n ), therefore, the time complexity mainly lies in finding the augmented path.
Here we introduce another dinci algorithm, which uses BFs to construct a hierarchy chart and then DFS to expand.
Template
# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> # include <vector> # include <queue> using namespace STD; # define del (a, B) memset (a, B, sizeof (A) const int n = 1000; const int INF = 0x3f3f3f; struct edge {int from,, cap, flow ;}; struct dinic {int n, m, S, T; vector <edge> edges; vector <int> G [N]; bool vis [N]; int d [N], cur [N]; void Init (int n) {This-> N = N; For (INT I = 0; I <= N; I ++) g [I]. clear (); edges. clear ();} void addedge (int from, int to, int cap) {// edge creation edges. push_back (edge) {from, to, Cap, 0}); edges. push_back (edge) {to, from, 0, 0}); M = edges. size (); G [from]. push_back (m-2); G [to]. push_back (m-1);} bool BFS () {del (VIS, 0); queue <int> q; q. push (s); D [s] = 0; vis [s] = 1; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <G [X]. size (); I ++) {edge & E = edges [G [x] [I]; If (! Vis [E. to] & E. cap> E. flow) {vis [E. to] = 1; d [E. to] = d [x] + 1; q. push (E. to) ;}}} return vis [T];} int DFS (int x, int A) {If (x = T | A = 0) return; int flow = 0, F; For (Int & I = cur [X]; I <G [X]. size (); I ++) {edge & E = edges [G [x] [I]; If (d [x] + 1 = d [E. to] & (F = DFS (E. to, min (A, E. cap-e.flow)> 0) {e. flow + = f; edges [G [x] [I] ^ 1]. flow-= f; flow + = f; A-= f; if (a = 0) break;} return flow;} int max_flow (int s, int t) {This-> S = s; this-> T = T; int flow = 0; while (BFS () {del (cur, 0 ); flow + = DFS (S, INF);} return flow ;}}; dinic solve; int main () {int n, m; while (~ Scanf ("% d", & M, & N) {solve. init (n); For (INT I = 0; I <m; I ++) {int x, y, z; scanf ("% d ", & X, & Y, & Z); solve. addedge (x, y, z);} printf ("% d \ n", solve. max_flow (1, N); // start point and end point} return 0 ;}