Pagini recente » Profil IulianOleniuc | Statistici Alexia Ichim (ialexia_ioana) | Cod sursa (job #3188940) | Cod sursa (job #1763065) | Cod sursa (job #2079367)
#include <stdio.h>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
const int INF = 1e6 ;
const int N = 1010 ;
int noNodes , noEdges ;
int remainingFlow [ N ][ N ];
int dist [ N ];
vector < int > muc [ N ] ;
int vecPoint [ N ] ;
int bfs (){
static int node ;
memset( dist , -1 , sizeof dist );
queue < int > que ;
dist [ 1 ] = 0 ;
que.push( 1 );
while ( !que.empty() ){
node = que.front() ;
que.pop() ;
for ( int vec : muc[ node ] ){
if ( dist [ vec ] == -1 && remainingFlow[ node ][ vec ] > 0 ){
dist [ vec ] = dist [ node ] + 1 ;
que.push( vec );
}
}
}
return dist [ noNodes ] != -1 ;
}
int dfs ( int node , int crFlow ){
if ( node == noNodes ){
return crFlow ;
}
auto it = muc [ node ].begin() ;
for ( advance( it , vecPoint [ node ] ) ; it != muc[ node ].end() ; it++ , vecPoint [ node ] ++ ){
int vec = *it ;
if ( remainingFlow [ node ] [ *it ] == 0 || dist [ vec ] != dist [ node ] + 1 ){
continue ;
}
int flow = dfs ( vec , min ( crFlow , remainingFlow [ node ][ *it ] ) );
if ( flow ){
remainingFlow [ node ][ vec ] -= flow ;
remainingFlow [ vec ][ node ] += flow ;
return flow ;
}
}
return 0 ;
}
int main(){
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&noNodes , &noEdges );
for ( int i = 0 ; i < noEdges ; i++ ){
int x , y , crCapacity ;
scanf("%d%d%d", &x , &y , &crCapacity );
remainingFlow [ x ][ y ] = crCapacity ;
if ( !remainingFlow [ y ][ x ] ){
muc [ x ].push_back( y );
muc [ y ].push_back( x );
}
}
int totalFlow = 0 ;
while ( bfs () ){
memset( vecPoint , 0 , sizeof vecPoint );
while ( int crFlow = dfs ( 1 , INF ) ){
totalFlow += crFlow ;
}
}
printf("%d",totalFlow);
return 0;
}