Pagini recente » Statistici Buzgariu Marius (mariusbuzgariu) | Cod sursa (job #1015969) | Cod sursa (job #2134751) | Istoria paginii runda/mmm | Cod sursa (job #1363914)
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 1000 + 1;
const int INF = 1e9;
int C[Nmax][Nmax];
int F[Nmax][Nmax];
vector<int> G[Nmax];
int tata[Nmax], coada[Nmax];
int N, M;
bool BFS(int S, int D)
{
fill(tata + 1, tata + N + 1, 0);
int st = 1, dr = 0;
coada[ ++dr ] = S;
tata[S] = S;
while ( st <= dr )
{
int nod = coada[ st++ ];
for(int son: G[nod])
{
if ( tata[son] == 0 && C[nod][son] > F[nod][son] )
{
tata[son] = nod;
coada[ ++dr ] = son;
if ( son == D )
return true;
}
}
}
return false;
}
int flux(int S, int D)
{
int flow = 0;
while ( BFS(S, D) )
{
for (int son: G[D])
{
if ( tata[son] == 0 || F[son][D] >= C[son][D] )
continue;
tata[D] = son;
int fmin = INF;
for ( int nod = D; nod != S; nod = tata[nod] )
fmin = min(fmin, C[ tata[nod] ][nod] - F[ tata[nod] ][nod] );
flow += fmin;
if ( !fmin ) continue;
for ( int nod = D; nod != S; nod = tata[nod] )
{
F[ tata[nod] ][nod] += fmin;
F[nod][ tata[nod] ] -= fmin;
}
}
}
return flow;
}
int main()
{
ifstream in("maxflow.in");
ofstream out("maxflow.out");
ios_base::sync_with_stdio(false);
in >> N >> M;
for ( int i = 1; i <= M; ++i )
{
int a, b, c;
in >> a >> b >> c;
G[a].push_back(b);
G[b].push_back(a);
C[a][b] = c;
}
out << flux(1, N) << "\n";
return 0;
}