Pagini recente » Cod sursa (job #2766923) | Cod sursa (job #2131115) | Cod sursa (job #614236) | Cod sursa (job #623588)
Cod sursa(job #623588)
# include <fstream>
# include <vector>
# include <algorithm>
# include <queue>
# define pb push_back
# define dim 1005
# define inf 9999
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
vector < int > a[ dim ];
int C[ dim ][ dim ], F[ dim ][ dim ];
int t[ dim ], viz[ dim ];
int flow = 0, maxflow = 0;
queue < int > q;
int n, m;
int minim( int x, int y )
{
if ( x < y )
return x;
return y;
}
void citire()
{
int i, x, y, z, j;
f >> n >> m;
for ( i = 1 ; i <= m ; i++ )
{
f >> x >> y >> z;
C[ x ][ y ] = z;
a[ x ].pb( y );
a[ y ].pb( x );
}
}
int bf()
{
int i, nod;
for ( i = 1 ; i <= n ; i++ )
viz[ i ] = 0;
q.push( 1 );
viz[ 1 ] = 1;
while ( !q.empty() )
{
nod = q.front();
if ( nod != n )
for ( i = 0 ; i < a[ nod ].size() ; i++ )
if( viz[ a[ nod ][ i ] ] == 0 && F[ nod ][ a[ nod ][ i ] ] < C[ nod ][ a[ nod ][ i ] ] )
{
viz[ a[ nod ][ i ] ] = 1;
t[ a[ nod ][ i ] ] = nod;
q.push( a[ nod ][ i ] );
}
q.pop();
}
return viz[ n ];
}
void rezolva()
{
int i, nod = 0, j;
while( bf () )
{
for ( i = 0 ; i < a[ n ].size() ; i++ )
if ( viz[ a[ n ][ i ] ] == 1 && F[ a[ n ][ i ] ][ n ] < C[ a[ n ][ i ] ][ n ] )
{
flow = inf;
t[ n ] = a[ n ][ i ];
nod = n;
while( nod != 1 )
{
flow = minim( flow, C[ t[ nod ] ][ nod ] - F[ t[ nod ] ][ nod ] );
nod = t[ nod ];
}
if( flow )
{
nod = n;
while( nod != 1 )
{
C[ t[ nod ] ][ nod ] -= flow;
C[ nod ][ t[ nod ] ] += flow;
nod = t[ nod ];
}
}
maxflow = maxflow + flow;
}
}
}
void afisare()
{
g << maxflow;
}
int main()
{
citire();
rezolva();
afisare();
return 0;
}