Pagini recente » Cod sursa (job #41480) | Cod sursa (job #2938937) | Cod sursa (job #2125002) | Cod sursa (job #1059752) | Cod sursa (job #1262769)
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 1002;
vector <int> graph [N];
bool used [N];
int c [N][N], f [N][N], last [N];
bool bfs (const int &s, const int &d) {
vector <int> :: iterator it;
int x;
queue <int> q;
q.push (s);
memset (used, 0, sizeof (used));
memset (last, 0, sizeof (last));
used [s] = 1;
last [s] = 0;
while (!q.empty ()) {
x = q.front ();
for (it = graph [x].begin (); it != graph [x].end (); ++ it)
if (!used [*it] && c [x][*it] - f [x][*it] > 0) {
q.push (*it);
last [*it] = x;
if (*it == d)
return 1;
}
q.pop ();
}
return 0;
}
int main () {
int n, m, x, y, cc, flow = 0, minflow, i;
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, &cc);
graph [x].push_back (y);
c [x][y] = cc;
}
while (bfs (1, n)) {
minflow = (1ll << 31) - 1;
for (i = n; i > 1; i = last [i])
minflow = min (minflow, c [last [i]][i] - f [last [i]][i]);
if (minflow == (1ll << 31) - 1)
break;
flow += minflow;
for (i = n; i > 1; i = last [i]) {
f [last [i]][i] += minflow;
f [i][last [i]] -= minflow;
}
}
printf ("%d\n", flow);
return 0;
}