Pagini recente » Cod sursa (job #783658) | Cod sursa (job #1311712) | Cod sursa (job #299666) | Cod sursa (job #1765019) | Cod sursa (job #1376724)
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
#include <fstream>
using namespace std;
const int MAXN = 1000, INF = 2000000000;
vector <int> G[MAXN+1];
int N, M, C[MAXN+1][MAXN+1], T[MAXN+1], viz[MAXN+1], maxFlow;
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;
}
}
void setViz0()
{
for(int i = 1; i <= N; i++)
viz[ i ] = 0;
}
bool findAugPath()
{
setViz0();
queue <int> c; c.push( 1 ); viz[ 1 ] = 1;
while( c.empty() == false )
{
int curr = c.front(); c.pop();
for(int i = 0; i < G[ curr ].size(); i++)
{
int next = G[ curr ][ i ];
if( viz[ next ] == 0 && C[ curr ][ next ] > 0 )
{
c.push( next ); viz[ next ] = 1; T[ next ] = curr;
if( next == N )
return true;
}
}
}
return false;
}
int minAugPath()
{
int nod = N, minF = INF;
for( ; nod != 1; nod = T[ nod ] )
minF = min( minF, C[ T[ nod ] ][ nod ] );
return minF;
}
void updateAugPath(int x)
{
for(int nod = N; nod != 1; nod = T[ nod ] )
C[ T[ nod ] ][ nod ] -= x, C[ nod ][ T[ nod ] ] +=x;
}
void findMaxFlow()
{
while( findAugPath() == true )
{
for(int i = 0; i < G[ N ].size(); i++)
{
int next = G[ N ][ i ];
if( viz[ next ] == 1 && C[ next ][ N ] > 0 )
{
T[ N ] = next;
int minF = minAugPath();
updateAugPath( minF );
maxFlow += minF;
}
}
}
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
citire();
findMaxFlow();
printf("%d",maxFlow);
return 0;
}