Pagini recente » Cod sursa (job #1674323) | Cod sursa (job #1154922) | Cod sursa (job #2540389) | Cod sursa (job #2871950) | Cod sursa (job #1965782)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
#define pb push_back
const int NMax = 1e3 + 5;
const int MMax = 5e3 + 5;
const int inf = 1e9 + 5;
int N,M;
int cap[NMax][NMax],dad[NMax];
vector<int> v[NMax];
bool bfs();
int main() {
in>>N>>M;
for (int i=1;i<=M;++i) {
int x,y,c;
in>>x>>y>>c;
cap[x][y] = c;
//cap[y][x] = 0;
v[x].pb(y);
v[y].pb(x);
}
int mxFlow = 0;
while (bfs()) {
int mnCap = inf, nod = N;
while (nod != 1) {
mnCap = min(mnCap,cap[dad[nod]][nod]);
nod = dad[nod];
}
nod = N;
while (nod != 1) {
cap[dad[nod]][nod] -= mnCap;
cap[nod][dad[nod]] += mnCap;
nod = dad[nod];
}
mxFlow += mnCap;
}
out<<mxFlow<<'\n';
in.close();out.close();
return 0;
}
bool bfs() {
bool viz[NMax] = {};
queue<int> Q;
viz[1] = true;
Q.push(1);
while (Q.size()) {
int nod = Q.front();
Q.pop();
for (int k=0;k < (int)v[nod].size();++k) {
int next = v[nod][k];
if (viz[next] || !cap[nod][next]) {
continue;
}
dad[next] = nod;
viz[next] = true;
Q.push(next);
if (next == N) {
return 1;
}
}
}
return 0;
}