43#ifndef __ADS_PushRelabel
44 #define __ADS_PushRelabel
126inline void FlowNetwork::pushRelabel(
int s ,
int t )
130 long long *
const R = f_res.data();
131 std::vector< long long > ex( N , 0 );
137 for(
const int x : f_imbNodes ) {
138 if( ( x != s ) && ( x != t ) && ( f_imb[ x ] > 0 ) ) {
139 ex[ x ] = f_imb[ x ];
145 f_unbalanced =
false;
147 if( ! any && ( inflow( t ) != 0 ) ) {
148 std::vector< char > seen( N , 0 );
149 std::vector< int > S = { s };
152 while( ! S.empty() && ! reach ) {
153 const int u = S.back();
155 for(
const auto & [ e , v ] : f_adj[ u ] )
156 if( ! seen[ v ] && ( R[ e ] > 0 ) ) {
158 reach = reach || ( v == t );
165 std::vector< int > d( N , N );
168 std::vector< int > aFirst( 2 * N + 1 , -1 ) , aNext( N );
169 std::vector< char > queued( N , 0 );
170 std::vector< int > bFirst( 2 * N + 1 , -1 ) , bNext( N ) , bPrev( N );
171 int aMax = -1 , bMax = -1;
172 auto aPush = [ & ](
int v ) {
176 aNext[ v ] = aFirst[ d[ v ] ];
177 aFirst[ d[ v ] ] = v;
181 auto bAdd = [ & ](
int v ) {
183 bNext[ v ] = bFirst[ d[ v ] ];
184 if( bNext[ v ] >= 0 )
185 bPrev[ bNext[ v ] ] = v;
186 bFirst[ d[ v ] ] = v;
190 auto bDel = [ & ](
int v ) {
191 if( bPrev[ v ] >= 0 )
192 bNext[ bPrev[ v ] ] = bNext[ v ];
194 bFirst[ d[ v ] ] = bNext[ v ];
195 if( bNext[ v ] >= 0 )
196 bPrev[ bNext[ v ] ] = bPrev[ v ];
202 int target = t , top = N;
203 std::vector< int > Q;
204 std::vector< char > inGame( N , 1 );
205 auto relabelAll = [ & ](
bool first ) {
206 int *
const D = d.data();
207 const char *
const G = inGame.data();
209 for(
int v = 0 ; v < N ; ++v )
213 want += ( ex[ v ] > 0 ) && ( v != s ) && ( v != t );
215 for(
int k = 0 ; k <= aMax ; ++k )
217 for(
int k = 0 ; k <= bMax ; ++k )
221 D[ s ] = D[ t ] = -1;
223 Q.resize( std::size_t( N ) );
224 int *
const q = Q.data();
226 q[
tail++ ] = target;
228 const bool all = target == t;
229 std::size_t level =
tail;
232 if(
head == level ) {
238 const int v = q[
head++ ];
239 const int dv = D[ v ] + 1;
240 const Incidence * a = f_adj[ v ].data();
241 const Incidence *
const z = a + f_adj[ v ].size();
245 for( ; a != z ; ++a ) {
247 const bool ok = ( D[ u ] == tp ) & ( R[ a->e ^ 1 ] > 0 );
248 D[ u ] = ok ? dv : D[ u ];
251 want -= ok & ( ex[ u ] > 0 );
254 for( ; a != z ; ++a ) {
259 if( G[ u ] && ( R[ e ^ 1 ] > 0 ) ) {
266 D[ target == t ? s : t ] = tp;
268 const int above = last + 2;
269 for(
int v = 0 ; v < N ; ++v )
270 if( G[ v ] && ( D[ v ] == tp ) && ( v != s ) && ( v != t ) &&
276 for( std::size_t i = 1 ; i <
tail ; ++i ) {
277 const int v = q[ i ];
284 const long long nm = 6LL * N +
static_cast< long long >( f_to.size() ) / 2;
290 auto run = [ & ](
bool global ) {
292 const int u = aFirst[ aMax ];
297 aFirst[ aMax ] = aNext[ u ];
299 const int du = d[ u ];
300 if( ( du != aMax ) || ( ex[ u ] == 0 ) )
302 long long x = ex[ u ];
304 const auto & A = f_adj[ u ];
305 for(
const auto & [ e , v ] : A ) {
306 const int dv = d[ v ];
307 if( ( dv >= du ) && ( dv >= nd ) )
309 const long long r = R[ e ];
313 const long long y = r < x ? r : x;
316 if( ( ex[ v ] == 0 ) && ( v != s ) && ( v != t ) )
330 work += 12 +
static_cast< long long >( A.size() );
332 if( bFirst[ du ] < 0 ) {
333 for(
int k = du + 1 ; k <= bMax ; ++k ) {
334 for(
int v = bFirst[ k ] ; v >= 0 ; v = bNext[ v ] )
342 d[ u ] = nd + 1 < top ? nd + 1 : top;
347 if( global && ( 2 * work > nm ) ) {
357 for(
const auto & [ e , v ] : f_adj[ s ] ) {
358 const long long r = R[ e ];
359 if( ( r > 0 ) && ( v != s ) ) {
372 for(
int v = 0 ; ( v < N ) && ! left ; ++v )
373 left = ( ex[ v ] > 0 ) && ( v != s ) && ( v != t ) && ( d[ v ] == top );
376 for(
int v = 0 ; v < N ; ++v )
377 inGame[ v ] = ( d[ v ] == top ) || ( v == s );
The maximum flow of section 15.2 in a network , with integer capacities, and the minimum cut.
int n(void) const
the number of nodes
Definition MaxFlow.h:135
int tail(int e) const
the tail of the edge number
Definition MaxFlow.h:165
int head(int e) const
the head of the edge number
Definition MaxFlow.h:172