Maximum network flow algorithm-Dinic algorithm and optimization, maximum flow dinic Algorithm
Overview
Dinic is the most commonly used algorithm for finding the largest stream in the network in the informatics competition.
It is favored by oier because of its intuitive thinking, low code difficulty, and superior performance.
Thoughts
$ Dinic $ algorithm belongsZengguang RoadAlgorithm.
Its core idea is:PointTo expand the connected edge. In the case of augmented, each time a great stream is added"
Unlike EK algorithms, EK algorithms start with edges, while Dinic algorithms start with vertices.
In the process of augmented reality, we try to expand the edge connected by a vertex, that isMulti-channel augmented
The Dinic algorithm also introducesLayered chartThis concept means that for a node $ I $, $ dis (I) $ is used to represent the distance from the node to the Source Vertex, and stipulates that an edge can be extended, when and only when it connects two vertices $ u, v $ satisfies: $ dis (v) = dis (u) + 1 $, the time complexity can be greatly optimized.
Implementation
With the above knowledge, the implementation of Dinic is relatively simple.
Each time BFS constructs a layered graph (note that it must be re-constructed every time, because some useless edges will be deleted after each enhancement, and some useless points will be deleted)
Then, the multi-path augmented mode starts from the source point.
Optimization
- Current Arc optimization:For each vertex, we will record the vertices that have been extended. When we return to this vertex again, we will ignore the vertices that have already been extended and expand from the next edge.
- Hierarchical Optimization (name from xjb): When layering, find the sink and quit immediately.
- Remaining amount optimization (from your own): If the node has no traffic during the augmented process, exit directly.
Time Complexity
The theoretical time complexity of the Dinic algorithm is $ O (n ^ 2 * m) $
Here is the proof.
But!
The performance of the Dinic algorithm is superior in the competition.
According to Dr. ly of the training team, we can consider that the time complexity of the Dinic algorithm isLinear(I don't know where to go from a label algorithm)
Code
Question Link
# Include <cstdio> # include <cstring> # include <queue> # define AddEdge (x, y, z) add_edge (x, y, z), add_edge (y, x, x, 0); using namespace std; const int MAXN = 1e6 + 1; const int INF = 1e8 + 10; inline char nc () {static char buf [MAXN], * p1 = buf, * p2 = buf; return p1 = p2 & (p2 = (p1 = buf) + fread (buf, 1, MAXN, stdin ), p1 = p2 )? EOF: * p1 ++;} inline int read () {char c = nc (); int x = 0, f = 1; while (c <'0' | c> '9') {if (c = '-') f =-1; c = nc ();} while (c> = '0' & c <= '9') {x = x * 10 + c-'0'; c = nc ();} return x * f;} int N, M, S, T; struct node {int v, flow, nxt;} edge [MAXN * 4]; int head [MAXN], cur [MAXN], num = 0; // note that inline void add_edge (int x, int y, int z) {edge [num] must start from 0. v = y; edge [num]. flow = z; edge [num]. nxt = head [x]; head [x] = num ++;} int deep [MAXN], q [MAXN]; inline bool B FS () {memset (deep, 0, sizeof (deep); deep [S] = 1; int l = 0, r = 1; q [++ l] = S; while (l <= r) {int p = q [l ++]; for (int I = head [p]; I! =-1; I = edge [I]. nxt) if (! Deep [edge [I]. v] & edge [I]. flow) {deep [edge [I]. v] = deep [p] + 1; q [++ r] = edge [I]. v; if (edge [I]. v = T) return 1; // returns 30 ms faster when the sink is found} return deep [T];} int DFS (int now, int nowflow) {if (now = T) return nowflow; int totflow = 0; // how much traffic can be extended from this point for (int I = head [now]; I! =-1; I = edge [I]. nxt) // The Current Arc optimization speed is 150 ms {if (deep [edge [I]. v] = deep [now] + 1 & edge [I]. flow) // only the points that meet the distance and traffic requirements can be extended {int canflow = DFS (edge [I]. v, min (nowflow, edge [I]. flow); edge [I]. flow-= canflow; edge [I ^ 1]. flow + = canflow; // augmented totflow + = canflow; nowflow-= canflow; if (nowflow <= 0) break; // no traffic at the current point is faster than 100 ms} return totflow;} void Dinic () {int ans = 0; while (BFS ()) // construct the hierarchy chart {memcpy (cur, head, sizeof (head) each time; // The ans ++ = DFS (S, INF) of the current arc optimization ); // extended} printf ("% d", ans);} int main () {# ifdef WIN32 freopen (". in "," r ", stdin); # else # endif N = read (); M = read (); S = read (); T = read (); memset (head,-1, sizeof (head); for (int I = 1; I <= M; I ++) {int x, y, z; x = read (); y = read (); z = read (); AddEdge (x, y, z);} Dinic (); return 0 ;}