Pagini recente » Cod sursa (job #20605) | Cod sursa (job #469347) | Cod sursa (job #771907) | Cod sursa (job #2435794) | Cod sursa (job #810875)
Cod sursa(job #810875)
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define NMAX 1005
#define INF 2e7;
int n , m , x ,y , z ,flx ,fluxmin;
vector< int > g[NMAX] ;
queue < int > que ;
int c[NMAX][NMAX]={0} , viz[NMAX] , t[NMAX] , flux[NMAX][NMAX] = { 0} ;
int bfs()
{
for(int i=1 ;i<=n; ++i ) viz[i]=0 ;
while(que.size())
que.pop();
que.push( 1 );
while( que.size() )
{
int x = que.front();
que.pop();
viz[ x ] = 1 ;
if( x == n ) continue ;
for(int j = 0 ; j < g [ x ].size() ; ++j )
{
int y = g [ x ][ j ] ;
if( !viz[ y ] && ( flux[ x ][ y ] < c[ x ][ y ] ) )
{
que.push(y);
t[ y ] = x ;
}
}
}
return viz[n];
}
void citeste()
{
scanf("%d %d",&n,&m);
for(int i = 1 ; i <= m ;++i)
{
scanf("%d %d %d",&x,&y,&z);
c[x][y] +=z;
g[x].push_back ( y );
g[y].push_back( x );
}
}
int main()
{
freopen("maxflow.in", "r",stdin);
freopen("maxflow.out","w",stdout);
citeste();
flx=0;
while( bfs() )
{
for( int i=0 ;i< g[n].size(); ++i )
{
int y=g[n][i];
if( !viz[y] || ( flux[ y ][ n ] == c[ y ][ n ]) )continue ;
t[n]=y;
fluxmin=INF;
for(int nod = n ; nod != 1 ; nod = t[nod])
fluxmin = min ( fluxmin , c[ t [ nod ] ] [ nod ] - flux [ t[nod] ] [ nod ] ) ;
if( fluxmin == 0 ) continue ;
for(int nod = n ; nod != 1 ; nod = t [ nod ] )
{
flux [ t[ nod ] ][nod] += fluxmin;
flux [nod] [ t[nod] ] -= fluxmin;
}
flx += fluxmin ;
}
}
printf("%d",flx);
return 0;
}