Pagini recente » Cod sursa (job #1200326) | Cod sursa (job #2191725) | Cod sursa (job #825036) | Cod sursa (job #907476) | Cod sursa (job #1368175)
#include <iostream>
#include <vector>
#include <deque>
#include <cstdio>
using namespace std;
const int MAXN = 1000, INF = 2000000000;
vector <int> G[MAXN+1];
int N, M, C[MAXN+1][MAXN+1], F[MAXN+1][MAXN+1], T[MAXN+1], viz[MAXN+1];
void citire()
{
scanf("%d %d",&N,&M);
for(int i = 1; i <= M; i++)
{
int x, y, z; scanf("%d %d %d",&x,&y, &z);
G[ x ].push_back( y ); G[ y ].push_back( x );
C[ x ][ y ] += z; F[ x ][ y ] = 0;
}
}
bool findAugPath()
{
for(int i = 1; i <= N; i++)
viz[ i ] = 0;
deque <int> c; c.push_back( 1 ); viz[ 1 ] = 1;
while( c.empty() == false )
{
int nc = c[ 0 ]; c.pop_front(); viz [ nc ] = 1;
for(int i = 0; i < G[ nc ].size(); i++)
{
int next = G[ nc ][ i ];
//if( viz[ next ] == 0 && ( C[ nc ][ next ] > 0 || F[ nc ][ next ] > 0 ) )
if( viz[ next ] == 0 && ( C[ nc ][ next ] > 0 ) )
{
T[ next ] = nc; c.push_back( next );
if( next == N )
return true;
}
}
}
return false;
}
int minOnAugPath()
{
int nod = N, minF = INF;
while( nod != 1 )
{
//cout<<nod<<' ';
minF = min( minF, C[ T[ nod ] ][ nod ] );
nod = T[ nod ];
}
//cout<<1<<' ';
//cout<<endl;
return minF;
}
void updateFlowOnAugPath(int f)
{
int nod = N;
while( nod != 1 )
{
C[ T[ nod ] ][ nod ] -= f;
F[ nod ][ T[ nod ] ] += f;
nod = T[ nod ];
}
}
int maxFlow = 0;
void findMaxFlow()
{
while( findAugPath() == true )
{
int f = minOnAugPath();
updateFlowOnAugPath( f );
maxFlow += f;
}
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
citire();
findMaxFlow();
cout<<maxFlow;
return 0;
}