Pagini recente » Cod sursa (job #3188173) | Cod sursa (job #2333550) | Cod sursa (job #376859) | Cod sursa (job #2755055) | Cod sursa (job #2155873)
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
ifstream f ("maxflow.in");
ofstream g ("maxflow.out");
vector < int > G[1005];
int n, m, inflow[1005][1005], outflow[1005][1005], i, j, a, b, c, mi, flux;
int fv[1005];
void update( int nod )
{
while ( nod!=1 )
{
outflow[ nod ][ fv[nod] ] += mi;
nod = fv[nod];
}
}
bool bfs()
{
mi=9999999;
queue < pair < int, int > > q;
pair < int, int > aux;
memset( fv, 0 , sizeof(fv) );
q.push( { 1, mi } );
while ( !q.empty() )
{
aux = q.front();
q.pop();
if ( aux.x == n )
{
mi = aux.y;
update(n);
return 1;
}
for ( int i = 0 ; i < G[aux.x].size() ; i++ )
{
if ( fv[G[aux.x][i]] == 0 && (inflow[aux.x][ G[aux.x][i] ] > outflow[G[aux.x][i]][ aux.x ]) )
{
fv[ G[aux.x][i] ] = aux.x ;
q.push( { G[aux.x][i] , min ( aux.y , inflow[aux.x][ G[aux.x][i] ] - outflow[ G[aux.x][i] ][ aux.x ] ) } );
}
}
}
return 0;
}
int main()
{
f>>n>>m;
for ( i = 1; i <= m ; i++ )
{
f>>a>>b>>c;
G[a].push_back(b);
inflow[a][b] += c;
}
while ( bfs() )
{
flux+=mi;
}
g<<flux;
}