Pagini recente » Cod sursa (job #2413505) | Cod sursa (job #2150781) | Cod sursa (job #1929208) | Cod sursa (job #1989513) | Cod sursa (job #2247073)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int a[MAXN][MAXN];
vector<int>G[MAXN];
int from[MAXN];
bool bfs(int s, int d) {
queue<int>q;
q.push(s);
memset(from, -1, sizeof(from));
from[s] = 0;
while (!q.empty() && from[d] == -1) {
int aux = q.front();
q.pop();
for (auto it:G[aux])
if (from[it] == -1 && a[aux][it] > 0) {
from[it] = aux;
q.push(it);
}
}
return (from[d] != -1);
}
int maxFlow(int s, int d) {
int ans = 0;
while (bfs(s, d)) {
for (auto it:G[d]) {
int minim = a[it][d];
if (minim == 0 || from[it] == -1)
continue;
int nod = it;
while (nod != s) {
minim = min(minim, a[from[nod]][nod]);
nod = from[nod];
}
nod = it;
a[it][nod] -= minim;
a[nod][it] += minim;
while (nod != s) {
a[from[nod]][nod] -= minim;
a[nod][from[nod]] += minim;
nod = from[nod];
}
nod = d;
ans += minim;
}
}
return ans;
}
int main() {
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
G[x].push_back(y);
G[y].push_back(x);
a[x][y] = c;
}
printf("%d\n", maxFlow(1, n));
return 0;
}