Pagini recente » Cod sursa (job #458140) | Cod sursa (job #1881638) | Cod sursa (job #3141769) | Cod sursa (job #832246) | Cod sursa (job #941622)
Cod sursa(job #941622)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
const int MAXN = 1010;
const int INF = 1 << 30;
int N, M, S, D;
vector <int> Graf[MAXN];
int C[1010][1010], F[1010][1010], Viz[1010], T[1010];
queue <int> Q;
bool BFS ()
{
vector <int> :: iterator it;
int nod;
for (nod = 1; nod <= N; nod ++)
Viz[nod] = 0;
Q.push (1);
Viz[1] = 1;
while (!Q.empty ()){
nod = Q.front ();
Q.pop ();
if (nod == N)
continue;
for (it = Graf[nod].begin (); it != Graf[nod].end (); ++ it)
if (!Viz[*it] && F[nod][*it] != C[nod][*it]){
T[*it] = nod;
Viz[*it] = 1;
Q.push (*it);
}
}
return Viz[N];
}
inline int flux ()
{
vector <int> :: iterator it;
int nod, mn, fm = 0, flow = 0;
while (BFS ())
for (it = Graf[N].begin (); it != Graf[N].end (); ++ it){
T[N] = *it;
fm = INF;
for (nod = N; nod != 1; nod = T[nod])
fm = min (fm, C[ T[nod] ][nod] - F[ T[nod] ][nod]);
if (!fm)
continue;
for (nod = N; nod != 1; nod = T[nod]){
F[ T[nod] ][nod] += fm;
F[nod][ T[nod] ] -= fm;
}
flow += fm;
}
return flow;
}
int main()
{
int i, a, b, c;
in >> N >> M;
while (M --){
in >> a >> b >> c;
Graf[a].push_back (b);
Graf[b].push_back (a);
C[a][b] = c;
}
out << flux ();
return 0;
}