Pagini recente » Cod sursa (job #1313491) | Cod sursa (job #2645389) | Cod sursa (job #3176047) | Monitorul de evaluare | Cod sursa (job #2416354)
#include <bits/stdc++.h>
using namespace std;
int timp;
const int INF = 1e9, MAXN = 1000;
int dist[1 + MAXN], nxt[1 + MAXN];
vector <int> gr[1 + MAXN];
int c[1 + MAXN][1 + MAXN];
int n;
inline int bfs () {
dist[1] = timp;
nxt[1] = 0;
queue <int> q;
q.push (1);
while (!q.empty ()) {
int nod = q.front ();
q.pop ();
for (auto vec : gr[nod])
if (c[nod][vec] > 0 && dist[vec] != timp) {
dist[vec] = timp;
q.push (vec);
nxt[vec] = nod;
}
}
return dist[n] == timp;
}
inline int update () {
int nod, flow, steps;
nod = n;
flow = INF;
steps = 0;
while (nod > 1) {
steps++;
flow = min (flow, c[nxt[nod]][nod]);
nod = nxt[nod];
if (steps > n * 2)
return 0;
}
nod = n;
while (nod > 1) {
c[nxt[nod]][nod] -= flow;
c[nod][nxt[nod]] += flow;
nod = nxt[nod];
}
return flow;
}
int main() {
int m, i, x ,y, cap, sol;
freopen ("maxflow.in", "r", stdin);
freopen ("maxflow.out", "w", stdout);
scanf ("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
scanf ("%d%d%d", &x, &y, &cap);
gr[x].push_back (y);
gr[y].push_back (x);
c[x][y] += cap;
}
sol = 0;
timp = 1;
while (bfs ()) {
for (auto fiu : gr[n]) {
nxt[n] = fiu;
sol += update ();
}
timp++;
}
printf ("%d\n", sol);
return 0;
}