[acm-icpc] dinic's algorithm

75
Dinic’s Algorithm 郭至軒(KuoE0[email protected] KuoE0.ch

Upload: chih-hsuan-kuo

Post on 15-May-2015

3.064 views

Category:

Documents


4 download

TRANSCRIPT

Maximum Flow

s

1

2

4

3

t

3

3

2

3

2

4

2

3

Maximum Flow

s

1

2

4

3

t

3/3

2/3

0/2

3/3

2/2

1/4

2/2

3/3

Concept

Level Graph

Blocking Flow

Level Graph

S t

1

2

3

4

0/10

0/10

0/4

0/8

0/9

0/10

0/10

Level Graph

S t

1

2

3

4

1

0/10

0/10

0/4

0/8

0/9

0/10

0/10

Level Graph

S t

1

2

3

4

1

2

2

0/10

0/10

0/4

0/8

0/9

0/10

0/10

Level Graph

S t

1

2

3

4

1

2

2

3

3

0/10

0/10

0/4

0/8

0/9

0/10

0/10

Level Graph

S t

1

2

3

4

1 4

2

2

3

3

0/10

0/10

0/4

0/8

0/9

0/10

0/10

Level Graph

S t

1

2

3

4

1 4

2

2

3

3

0/10

0/10

0/4

0/8

0/9

0/10

0/10

s

1

2

3

4

t

Blocking Flow

...0/20

0/4

...

0/5

0/5 0/10

0/3 0/8

Blocking Flow

...0/20

0/4

...

0/5

0/5 0/10

0/3 0/8

Blocking Flow

...0/20

0/4

...

0/5

0/5 0/10

0/3 0/8

bottleneck: 4

Blocking Flow

......0/5 0/10

0/3 0/8

4/20

4/4 4/5

Blocking Flow

......0/5 0/10

0/3 0/8

4/20

4/4 4/5

Blocking Flow

......0/5 0/10

0/3 0/8

4/20

4/4 4/5

bottleneck: 5

Blocking Flow

......

0/3 0/8

4/4 4/5

5/5 5/109/20

Blocking Flow

......

0/3 0/8

4/4 4/5

5/5 5/109/20

Blocking Flow

......

0/3 0/8

4/4 4/5

5/5 5/109/20

bottleneck: 3

Blocking Flow

......

4/4 4/5

5/5 5/10

3/3 3/8

12/20

Blocking Flow

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

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

Example0/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

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

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

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

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

10/107/10

5/5 5/6

5/52/5

20/30 5/5

7/72/5

15/15 10/10

10/15

4

5/10

Maximum Flow: 27

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

Thank You for Your Listening.