[acm-icpc] dinic's algorithm
TRANSCRIPT
Dinic’s Algorithm郭至軒(KuoE0)
Latest update: Mar 27, 2013
Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)
http://creativecommons.org/licenses/by-sa/3.0/
Blocking Flow
1
2
2
3
3
5
3
4
4
4
10/10
10/15
15/15
5/10
7/7
5/5
2/5
5/510/10
3/5
5/5 5/6
8/10
20/30
Algorithm1. build a level graph
2. find an augmenting path from source to sink
3. find the bottleneck on augmenting path
4. find the augmenting path from bottleneck to sink
5. repeat step 3 and step 4 to construct a blocking flow until no augmenting path found
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
build a level graph
1
2
2
current flow: 0
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
build a level graph
1
2
2
3
3
3
current flow: 0
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
build a level graph
1
2
2
3
3
3
4
4
4
current flow: 0
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
build a level graph
1
2
2
3
3
3
4
4
4
5
current flow: 0
1
2
2
3
3
5
3
4
4
4
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
construct a blocking flow
1
2
5
3 4
current flow: 0
1
2
2
3
3
5
3
4
4
4
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
construct a blocking flow
1
2
5
3 4
current flow: 0
1
2
2
3
3
5
3
4
4
4
0/10
0/15
0/15
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
0/30
construct a blocking flow
1
2
5
3 4
current flow: 0
bottleneck: 10
1
2
2
3
3
5
3
4
4
4
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 0current flow: 10
10/10
10/15
10/15
10/30
10/10
10/15
10/15
10/30
1
2
2
3
3
5
3
4
4
4
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10
10/10
10/15
10/15
10/30
1
2
2
3
3
5
3
4
4
4
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10
10/10
10/15
10/15
10/30
1
2
2
3
3
5
3
4
4
4
0/10
0/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10
bottleneck: 5
10/10
10/15
1
2
2
3
3
5
3
4
4
4
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10
15/15
15/30
5/7
current flow: 15
5/10
15/15
15/30
10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
5/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10current flow: 15
3 4
15/15
15/30
10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
5/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10current flow: 15
3 4
15/15
15/30
10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
5/7
0/5
0/5
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10
bottleneck: 2
current flow: 15
3 4
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 10current flow: 15
3 4
17/30
7/7
2/5
2/5
current flow: 17
current flow: 17
17/30
7/7
2/5
2/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 17
3 4
current flow: 17
17/30
7/7
2/5
2/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 17
3 43
4
current flow: 17
17/30
7/7
2/5
2/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 17
3 43
4
current flow: 17
17/30
7/7
2/5
2/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/50/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 17
3 43
4
bottleneck: 3
current flow: 17
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
current flow: 17
3 43
4
20/30 5/5
3/53/10
current flow: 20
current flow: 20
20/30 5/5
3/53/10
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
3 43
4
current flow: 20
20/30 5/5
3/53/10
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
current flow: 20
20/30 5/5
3/53/10
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
current flow: 20
20/30 5/5
3/53/10
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5
0/5 0/6
0/10
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
bottleneck: 2
current flow: 20
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5 0/6
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
5/55/10
2/52/10
current flow: 22
current flow: 22
5/55/10
2/52/10
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5 0/6
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
current flow: 22
5/55/10
2/52/10
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5 0/6
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
3
4
current flow: 22
5/55/10
2/52/10
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5 0/6
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
3
4
current flow: 22
5/55/10
2/52/10
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
0/5 0/6
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
3
4
bottleneck: 5
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
construct a blocking flow
1
2
5
3 4
3 43
42
3
4
3
4
current flow: 27
10/107/10
5/5 5/6
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
1
2
2
3
3
5
3
4
4
4
5/10
no augmenting path from source to sink
1
2
5
3 4
3 43
42
3
4
3
4
current flow: 27
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
build a level graph
current flow: 27
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
build a level graph
current flow: 27
1
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
build a level graph
current flow: 27
1
2
2
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
build a level graph
current flow: 27
1
2
2
3
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
build a level graph
current flow: 27
1
2
2
3 4
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
build a level graph
current flow: 27
1
2
2
3 4
5
10/107/10
5/5 5/6
current flow: 22
5/52/5
20/30 5/5
7/72/5
15/15 10/10
10/15
4
5/10
no any path from source to sink exists
current flow: 27
1
2
2
3 4
5
Source Code
// vertex[ a ] is the adjacent list of a.// cap[ a ][ b ] is the capacity from a to b.// flow[ a ][ b ] is the occupied flow from a to b.// level[ a ] is the level in level graph of a.// path[ a ] is the previous node of a.
int Dinic( int source, int sink ) {int ret = 0;while ( buildLevelGraph( source, sink ) )
ret += constructBlockingFlow( source, sink );
return ret;}
Source Code
bool buildLevelGraph( int source, int sink ) {queue< int > que;memset( level, 0, sizeof( level ) );que.push( source );level[ source ] = 1;while ( que.empty() != true ) {
int now = que.front();que.pop();for ( int i = 0; i < vertex[ now ].size(); ++i ) {
int next = vertex[ now ][ i ];if ( ( cap[ now ][ next ] - flow[ now ][ next ] > 0 || flow[ next ][ now ] > 0 ) && level[ next ] == 0 ) {
que.push( next );level[ next ] = level[ now ] + 1;
}}
}return level[ sink ] != 0;
}
Source Codeint constructBlockingFlow( int source, int sink ) {
int ret = 0;stack< int > stk;memset( visit, 0, sizeof( visit ) );stk.push( source );while ( stk.empty() != true ) {
int now = stk.top();if ( now != sink ) {
for ( int i = 0; i < vertex[ now ].size() && stk.top() != now; ++i ) {
int next = vertex[ now ][ i ];if ( visit[ next ] || level[ next ] != level [ now ] + 1 )
continue;if ( cap[ now ][ next ] - flow[ now ][ next ] > 0 )
stk.push( next ), path[ next ] = now;else if ( flow[ now ][ next ] > 0 )
stk.push( next ), path[ next ] = -now;}
Source Codeif ( stk.top() == now )
stk.pop(), visit[ now ] = 1;}else {
int F = 1e9, bottleneck;for ( int cur = sink; cur != source; cur = abs( path[ cur ] ) )
F = min( F, path[ cur ] > 0 ? cap[ path[ cur ] ][ cur ] - flow[ path[ cur ] ][ cur ] : flow[ cur ][ -path[ cur ] ] );
for ( int cur = sink; cur != source; cur = abs( path[ cur ] ) ) {
if ( path[ cur ] > 0 ) {flow[ path[ cur ] ][ cur ] += F;if ( cap[ path[ cur ] ][ cur ] - flow[ path[ cur ] ][ cur ] == 0 )
bottleneck = path[ cur ];}
Source Code
else {flow[ cur ][ -path[ cur ] ] -= F;if ( flow[ cur ][ -path[ cur ] ] == 0 )
bottleneck = -path[ cur ];}
} while ( stk.empty() != true && stk.top() != bottleneck )
stk.pop();ret += F;
}}return ret;
}
Practice Now[POJ] 1459 - Power Network
Reference
•演算法筆記- Flow
• Maximum flow problem - Wikipedia, the free encyclopedia