1. Edmonds-Karp Algorithm
The extended path P is calculated using the breadth-first search, that is, if the augmented inbound path is the shortest path from (s to T in the residual network, it can improve the bounds of the FORD-FULKERSON,
This implementation of the Ford-Fulkerson method is called the Edmonds-Karp Algorithm, and the time complexity is O (VE ^ 2 );
HDU 3549
View code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <deque>
using namespace std;
int cap[100][100];
int a[100];
int flow[100][100];
int path[1010];
int N,M;
const int inf = 0x7f7f7f7f;
int f;
int Scan()
{
int res = 0 , ch ;
while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
{
if( ch == EOF ) return 1 << 30 ;
}
res = ch - '0' ;
while( ( ch = getchar() ) >= '0' && ch <= '9' )
res = res * 10 + ( ch - '0' ) ;
return res ;
}
void BFS( )
{
int i, j, k, t, u, v;
f = 0;
memset(flow, 0, sizeof(flow));
for (; ; )
{
memset(a, 0, sizeof(a));
memset(path, 0, sizeof(path));
deque<int>q;
a[1] = inf;
q.push_back(1);
while (!q.empty( ))
{
t = q.front( );
q.pop_front( );
for (i = 0; i <= N; i++)
if (!a[i] && flow[t][i] < cap[t][i] )
{
path[i] = t;
a[i] = min(a[t], cap[t][i] - flow[t][i]);
q.push_back(i);
}
}
if (a[N] == 0)
break;
for (u = N; u != 1; u = path[u])
{
flow[path[u]][u] += a[N];
flow[u][path[u]] -= a[N];
}
f += a[N];
}
printf("%d\n",f);
}
int main( )
{
int T, l = 0, i, b, c, d;
scanf("%d", &T);
while (T--)
{
l++;
scanf("%d%d", &N, &M);
memset(cap, 0, sizeof(cap));
for (i = 0; i < M; i++) {
//scanf("%d%d%d", &d, &b, &c);
d = Scan();
b = Scan();
c = Scan();
cap[d][b] += c;
}
printf("Case %d: ",l);
BFS( );
}
return 0;
}
2. Algorithm for pressing and remarking
In the introduction to algorithms, the most rapid implementation is based on the method of pressing and remarking.
There are several algorithms using this method:
1. General Pre-flow propulsion Algorithms
In the residual network, a pre-stream is maintained, and active points are constantly pushed or release to be re-adjusted.
The entire reservation is made until the operation fails. Time complexity O (V * E ).
2. first-in-first-out queue reserved propulsion Algorithm
In the residual network, the active point is maintained on the first-in-first-out column, and the time complexity is O (V * V );
3. hlpp)
In the residual network, Priority Queues are required for each highest-label active point check,
Time complexity O (V * E ^ 0.5)
View code
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <deque>
Using namespace STD;
Deque <int> q;
Int CAP [16] [16]; // storage network capacity C. Originally, the array was opened for 1000, and the Tle took n long.
Int flow [16] [16]; // Storage Flow Capacity F
Int e [16]; // The remainder stream of each vertex
Int H [16]; // The height of each fixed point
Int visit [16]; // marker Vertex
Int N, vertix;
Int scan ()
{
Int res = 0, ch;
While (! (CH = getchar ()> = '0' & Ch <= '9 '))
{
If (CH = EOF) return 1 <30;
}
Res = CH-'0 ';
While (CH = getchar ()> = '0' & Ch <= '9 ')
Res = res * 10 + (CH-'0 ');
Return res;
}
Const int INF = 0x7f7f7f7f;
Int min (int x, int y)
{
Return x <Y? X: Y;
}
Void push (int u, int V)
{
Int x = CAP [u] [v]-flow [u] [v];
If (H [u] = H [v] + 1 & E [u]> 0 & x> 0)
{
X = min (E [u], X );
Flow [u] [v] + = X;
Flow [v] [u] =-flow [u] [v];
E [u]-= X;
E [v] + = X;
If (! Visit [v] & V! = 1 & V! = Vertix)
{
Q. push_back (v );
Visit [v] = 1;
}
}
}
Void relable (int u)
{
Int H = inf;
If (E [u]> 0 & U! = 1 & U! = Vertix)
{
For (INT I = 1; I <= vertix; I ++)
{
Int x = CAP [u] [I]-flow [u] [I];
If (x> 0 & H [I] {
H = H [I];
}
}
If (H [u] <= H)
{
H [u] = H + 1;
Visit [u] = 1;
Q. push_back (U );
}
}
}
Void initialize_preflow ()
{
Memset (H, 0, sizeof (h ));
Memset (E, 0, sizeof (e ));
Memset (flow, 0, sizeof (flow ));
Memset (visit, 0, sizeof (visit ));
H [1] = vertix;
Visit [1] = 1;
Q. push_back (1 );
For (INT I = 1; I <= vertix; I ++)
{
If (Cap [1] [I])
{
Flow [1] [I] = CAP [1] [I];
Flow [I] [1] =-cap [1] [I];
E [I] = CAP [1] [I];
E [1] = E [1]-cap [1] [I];
Visit [I] = 1;
Q. push_back (I );
}
}
}
Void solve (int x)
{
While (! Q. Empty ())
{
Int u = Q. Front ();
Visit [u] = 0;
Q. pop_front ();
For (INT I = 1; I <= vertix; I ++)
{
Push (u, I );
}
Relable (U );
}
Int ans = 0;
For (INT I = 1; I <= vertix; I ++)
{
Ans + = flow [1] [I];
}
Printf ("case % d: % d \ n", X, ANS );
}
Int main ()
{
Int A, B, C, L = 0, T;
Scanf ("% d", & T );
While (t --)
{
Scanf ("% d", & vertix, & N );
L ++;
Q. Clear ();
Memset (Cap, 0, sizeof (CAP ));
For (INT I = 1; I <= N; I ++)
{
// Scanf ("% d", & A, & B, & C );
A = scan ();
B = scan ();
C = scan ();
Cap [a] [B] + = C;
}
Initialize_preflow ();
Solve (L );
}
Return 0;
}
/*
6
6
10
1 2 16
1 3 13
2 3 10
3 2 4
2 4 12
4 3 9
3 5 14
5 4 7
5 6 4
4 6 20
*/