Use Ford-Fulkerson labeling method to seek the maximum network.
①C, F initialized to INF to indicate that the edge does not exist
#include <iOSTREAM>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <omanip>
#include <algorithm>
#Define Maxn 10010
#Define Inf 0XFFFFFFFF
using namespace std;
Struct Arctype
{{
int c, f; // capacity, traffic
};
Arctype Edge [maxn] [maxn];
int n, m; // apex number, arc number
int Flag [maxn]; // Venture: -1 — unlimited; 0 — has not been checked; 1 — the label has been checked
int Prev [maxn]; // The first component of the label: indicate which one of the bid comes from to find out the amount of improvement
int Alpha [maxn]; // The second component of the label: the improvement amount α
int que [maxn]; // equivalent to the queue in BFS
int V; // Crane head element
int QS, QE; // The position of the team's head team
int i, j;
void form () // Label method to seek the maximum network of networks
{{
While (1) // label until there is no way to improve
{{
Memset (flag, -1, sizeof (flag));
Memset (prev, -1, sizeof (prev));
Memset (alpha, -1, sizeof (alpha));
flag [0] = 0;
prev [0] = 0;
alpha [0] = INF;
qs = qe = 0;
que [qe] = 0; // Source point 0 enter the queue
++ qe;
While (qs <qe && Flag [n-1] ==-1)
{{
v = que [qs]; // take the team's first element
++ qs;
for (i = 0; i <n; ++ i) // Check the positive and reverse adjacent points of vertex v
if (flag [i] ==-1)
{{
if (edge [v] [i] .c <inf && edge [v] [i] .f <edge [v] [i] .c) //
{{
Flag [i] = 0; // Give the vertex I, but it was not checked at this time
prev [i] = v;
alpha [i] = min (alpha [v], edge [v] [i] .c-edge [v] [i] .f);
que [qe] = i; // Spertencies I join the team
++ qe;
}
else if (edge [i] [v] .c <inf && edge [i] [v] .f> 0) // reverse and traffic
{{
flag [i] = 0;
prev [i] =-v;
alpha [i] = min (alpha [v], edge [i] [v] .f);
que [qe] = i;
++ qe;
}
}
Flag [v] = 1; // Tag vertex i has been checked
}
if (FLAG [N-1] ==-1 || Alpha [n-1] == 0) Break; // The adjustment of the exchange point without labeling or exchange point is 0
int k1 = n-1, k2 = abs (prev [k1]);
int a = alpha [n-1]; // The improved amount α
While (1)
{{
if (edge [k2] [k1] .f <inf) edge [k2] [k1] .f+= a; // positive
else edge [k1] [k2] .f- = a; // reverse
if (k2 == 0) break; // Always adjust to the source 0
k1 = k2;
k2 = abs (prev [k2]);
}
}
int maxflow = 0; // maximum traffic
For (i = 0; i <n; ++ i)
for (j = 0; j <n; ++ j)
{{
if (i == 0 && edge [i] [j] .f <inf) maxflow+= edge [i] [j] .f;
if (edge [i] [j] .f <inf) core << i << "->" << j <":" edge [i] .f << Endl;
}
COUT << "Maxflow:" Maxflow << Endl;
}
int Main ()
{{
int U, V, C, F; // The starting point of the arc at the end of the capacity flow
cin >> n >> m; // The number of apex, the number of arcs
For (i = 0; i <n; ++ i)
for (j = 0; j <n; ++ j)
edge [i] [j] .c = edge [i] [j] .f = inf; // Initialization, INF indicated that it is boundless connected
For (i = 0; i <m; ++ i)
{{
cin >> u >> v >> c >> f;
edge [u] [v] .c = c; // Constructing adjacent matrix
edge [u] [v] .f = f;
}
form ().
Return 0;
}
/*
6 10
0 1 8 2
0 2 4 3
1 3 2 2
1 4 2 2
2 1 4 2
2 3 1 1
2 4 4 0
3 4 6 0
3 5 9 3
4 5 7 2
*/
② not initialization
void form () // Label method to seek the maximum network flow of the network
{{
int Flow [maxn] [maxn]; // The traffic between nodes fIJ
int Prev [maxn]; // can improve the label of a node before the improvement path, which is equivalent to the first weight of the label
int Minflow [maxn]; // The improved amount α of each vertex, equivalent to the second weight of the label
int que [maxn];
int QS, QE; // The first position coordinates of the queue
int v, p; // current vertices, saving CIJ-FIJ
For (i = 0; I <maxn; ++ i)
for (j = 0; j <maxn; ++ j)
Flow [i] [j] = 0;
minflow [0] = INF; // The second component of the source label is infinite
While (1) // Label method
{{
For (i = 0; i <maxn; ++ i) // Before the mark, each vertex return to the uninvited state
prev [i] = -2;
prev [0] =-1;
qs = 0;
que [qs] = 0; // Source entering the team
qe = 1;
While (qs <qe && prev [t] ==-2)
{{
v = que [qs]; // Take the queue head node
++ qs;
For (i = 0; i <t+1; ++ i) // prev [i] ==-2 indicates that vertex I did not marked
if (prev [i] ==-2 && (p = edge [v] [i] .c-flow [v] [i]) // edge [v] [i] .c-flow [v] [i ] = 0 to ensure that I is a vertex of V neighboring and can be labeled
{{
prev [i] = v;
que [qe] = i;
++ qe;
minflow [i] = (minflow [v] <p)? MINFlow [v]: p;
}
}
if (prev [t] ==-2) break; // The exchange point T has no label, the label method is over
For (i = prev [t], j = t; i! =-1; j = i, i = prev [i]) // adjustment process
{{
Flow [i] [j]+= minflow [t];
flow [j] [i] = -fow [i] [j];
}
}
For (i = 0, p = 0; i <t; ++ i) // Statistics The traffic that enters the exchange point is the maximum flow of flow
p+= flow [i] [t];
core << P << Endl;
}