Pagini recente » Cod sursa (job #2868914) | Cod sursa (job #1304420) | Cod sursa (job #3156875) | Cod sursa (job #1940566) | Cod sursa (job #595967)
Cod sursa(job #595967)
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <cstdio>
#define Infinit 2000000000
using namespace std;
vector <int> G[1005];
int N, Flow[1005][1005], Father[1005], MaxFlow, Cap[1005][1005];
int Viz[1005];
deque <int> Coada;
int BF()
{
int i, j, V;
deque <int> :: iterator nod;
Coada.push_back (1);
memset(Viz, 0, sizeof(Viz));
Viz[1] = 1;
while (Coada.size ()>0)
{
nod = Coada.begin ();
if (*nod == N) continue;
for (j = 0; j < G[*nod].size (); j++)
{
V = G[*nod][j];
if (Cap[*nod][V] == Flow[*nod][V] || Viz[V]) continue;
Viz[V] = 1;
Coada.push_back (V);
Father[V] = *nod;
}
Coada.pop_front ();
}
return Viz[N];
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int i, flow, fmin, x, y, z, nod, M;
scanf("%d %d ", &N, &M);
for (i = 1; i <= M; i++)
{
scanf("%d %d %d ", &x, &y, &z);
Cap[x][y] += z;
G[x].push_back(y);
G[y].push_back(x);
}
for (flow = 0; BF();)
for (i = 0; i < G[N].size (); i++)
{
nod = G[N][i];
if (Flow[nod][N] == Cap[nod][N] || !Viz[nod]) continue;
Father[N] = nod;
fmin = Infinit;
for (nod = N; nod != 1; nod = Father[nod])
fmin = min(fmin, Cap[ Father[nod] ][nod] - Flow[ Father[nod] ][nod]);
if (fmin == 0) continue;
for (nod = N; nod != 1; nod = Father[nod])
{
Flow[ Father[nod] ][nod] += fmin;
Flow[nod][ Father[nod] ] -= fmin;
}
flow += fmin;
}
printf("%d ", flow);
return 0;
}