Pagini recente » Cod sursa (job #94890) | Cod sursa (job #77031) | Cod sursa (job #1574477) | Cod sursa (job #1470743) | Cod sursa (job #1061768)
#include <cstdio>
#include <vector>
#include <bitset>
#include <algorithm>
#include <queue>
#define node first
#define _node second
using namespace std;
const int NMAX = 1003, MMAX = 10003, INFI = 2e9;
vector <int> G[NMAX];
queue <int> Q;
bitset <NMAX> viz;
pair <int, int> edge[MMAX];
int cap[NMAX][NMAX], res[NMAX][NMAX], path[NMAX], N;
bool BFS () {
vector <int> :: iterator i;
int node;
viz.reset ();
viz[1] = 1;
for (Q.push (1); !Q.empty (); Q.pop ()) {
node = Q.front ();
if (node == N)
continue;
for (i = G[node].begin (); i != G[node].end (); ++i)
if (!viz[*i] && res[node][*i] < cap[node][*i]) {
viz[*i] = 1;
Q.push (*i);
path[*i] = node;
}
}
return viz[N];
}
int main () {
freopen ("maxflow.in", "r", stdin);
freopen ("maxflow.out", "w", stdout);
vector <int> :: iterator j;
int M, i, _min, f_max = 0;
scanf ("%d%d", &N, &M);
for (i = 1; i <= M; ++i) {
scanf ("%d%d", &edge[i].node, &edge[i]._node);
scanf ("%d", &cap[edge[i].node][edge[i]._node]);
G[edge[i].node].push_back (edge[i]._node);
G[edge[i]._node].push_back (edge[i].node);
}
while (BFS ())
for (j = G[N].begin (); j != G[N].end (); ++j) {
if (!viz[*j] || cap[*j][N] == res[*j][N])
continue;
path[N] = *j;
_min = INFI;
for (i = N; i != 1; i = path[i])
_min = min (_min, cap[path[i]][i] - res[path[i]][i]);
for (i = N; i != 1; i = path[i]) {
res[path[i]][i] += _min;
res[i][path[i]] -= _min;
}
f_max += _min;
}
printf ("%d", f_max);
}