Pagini recente » Cod sursa (job #520260) | Cod sursa (job #3227116) | Cod sursa (job #529016) | Cod sursa (job #2894608) | Cod sursa (job #2821904)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
#define inf (1<<30)
const int N=1003;
const int M=5003;
int n, s, t, m, F, f[N][N], h[N], maxflow;
vector <int> v[N];
queue <int> q;
int bfs () {
q.push(1);
h[1] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i];
if (h[y] || f[x][y] <= 0)
continue;
h[y] = h[x] + 1;
q.push(y);
}
}
return h[n] > 0;
}
int dfs (int x, int rez) {
if (x == t)
return rez;
for (int i = 0; i < v[x].size(); i++) {
int y = v[x][i];
if (h[y] != h[x] + 1 || f[x][y] <= 0)
continue;
int now = dfs(y, min(f[x][y], rez));
if (now > 0) {
f[x][y] -= now;
f[y][x] += now;
return now;
}
h[y] = inf;
}
return 0;
}
int main() {
in >> n >> m;
int x, y, c;
for (int i = 0; i < m; i++) {
in >> x >> y >> c;
v[x].emplace_back(y);
v[y].emplace_back(x);
f[x][y] += c;
}
s = 1, t = n;
while (true) {
for (int i = 1; i <= n; i++)
h[i] = 0;
if (!bfs())
break;
F = 0;
do {
F = dfs(s, inf);
maxflow += F;
}while (F);
}
out << maxflow;
return 0;
}