Pagini recente » Cod sursa (job #2136624) | Cod sursa (job #1408315) | Cod sursa (job #639214) | Cod sursa (job #1990002) | Cod sursa (job #1165578)
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 1001;
const int inf = (1ll << 31) - 1;
int n, m;
int capacity [N][N], flow [N][N], father [N];
vector <int> graph [N];
bool used [N];
queue <int> q;
bool bfs (const int &s, const int &d) {
int node;
vector <int> :: iterator it;
memset (used, 0, sizeof (used));
q.push (s);
used [s] = 1;
while (!q.empty ()) {
node = q.front ();
q.pop ();
for (it = graph [node].begin (); it != graph [node].end (); ++ it)
if (!used [*it] && capacity [node][*it] - flow [node][*it] > 0) {
father [*it] = node;
used [*it] = 1;
q.push (*it);
}
}
return used [d];
}
int maxFlow (const int &s, const int &d) {
int i, ans = 0, minim, node;
vector <int> :: iterator it;
while (bfs (s, d)) {
minim = inf;
for (it = graph [d].begin (); it != graph [d].end (); ++ it) {
node = *it;
if (flow [node][d] == capacity [node][d])
continue;
if (!used [node])
continue;
for (i = d; i != s; i = father [i])
if (minim > capacity [father [i]][i] - flow [father [i]][i])
minim = capacity [father [i]][i] - flow [father [i]][i];
if (minim == 0)
continue;
for (i = d; i != s; i = father [i]) {
flow [father [i]][i] += minim;
flow [i][father [i]] -= minim;
}
ans += minim;
}
}
return ans;
}
int main () {
int i, x, y, c;
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, &c);
graph [x].push_back (y);
graph [y].push_back (x);
capacity [x][y] = c;
}
printf ("%d", maxFlow (1, n));
return 0;
}