Pagini recente » Cod sursa (job #1708791) | Cod sursa (job #1206424) | Cod sursa (job #2504071) | Cod sursa (job #244206) | Cod sursa (job #2156320)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("maxflow.in");
ofstream g ("maxflow.out");
int n, m, a, b,ok, c, flux[1005][1005],cap[1005][1005],nod, pa[1005], oo = 99999999, mi, rsp;
vector < int > G[1005];
bool bfs ()
{
memset( pa, 0 , sizeof(pa) );
queue < int > q;
q.push( 1 );
pa[1] = -1;
while ( !q.empty() )
{
int aux = q.front();
q.pop();
for ( auto it : G[aux] )
{
if ( flux[ aux ][ it ] == cap[ aux ][ it ] )
{
continue;
}
if ( it != n && pa[ it ] == 0 )
{
pa[ it ] = aux;
q.push( it );
}
else if ( it == n )
{
ok = 1;
}
}
}
return ok;
}
int dinitz ()
{
do
{
ok = 0;
bfs();
for ( auto it : G[ n ] )
{
mi = oo;
if ( pa[ it ] != 0 && cap[ it ][ n ] > flux[ it ][ n ] )
{
pa[ n ] = it;
}
else
{
continue;
}
for ( nod = n ; nod!=1 ; nod = pa[nod] )
{
mi = min ( mi , cap[ pa[nod] ][ nod ] - flux[ pa[nod] ][ nod ] );
}
for ( nod = n ; nod!=1 ; nod = pa[nod] )
{
flux[ pa[nod] ][ nod ] += mi;
flux[ nod ][ pa[nod] ] -= mi;
}
rsp+=mi;
}
} while ( ok );
return rsp;
}
int main()
{
f>>n>>m;
for ( int i = 1; i <= m ; i++ )
{
f>>a>>b>>c;
G[a].push_back(b);
G[b].push_back(a);
cap[a][b] = c;
}
g<<dinitz();
return 0;
}