Pagini recente » Cod sursa (job #1024831) | Cod sursa (job #2710979) | Cod sursa (job #2193091) | Cod sursa (job #2425269) | Cod sursa (job #1897564)
#include<bits/stdc++.h>
#define in f
#define out g
using namespace std;
ifstream f ("maxflow.in");
ofstream g ("maxflow.out");
int const MAXSIZE = 1010;
int const INF = 20000000;
int n, m, viz[MAXSIZE];
int father[MAXSIZE];
vector<int> v[MAXSIZE];
int cp[MAXSIZE][MAXSIZE];
int x, y, z, k = INF;
long long s;
int bfs () {
queue<int> q;
q.push(1);
viz[1]++;
while (!q.empty()) {
int node = q.front();
if (node == n)
return 1;
q.pop();
for (int i = 0; i < v[node].size(); i++) {
int ngh = v[node][i];
if (viz[ngh] == 0 && cp[node][ngh] > 0) {
viz[ngh] = true;
father[ngh] = node;
q.push(ngh);
}
}
}
return 0;
}
int main() {
in >> n >> m;
for (int i = 1; i <= m; i++) {
in >> x >> y >> z;
cp[x][y] = z;
v[x].push_back(y);
}
while (bfs() == 1) {
int node = n;
while (father[node] != 0) {
k = min(k, cp[father[node]][node]);
node = father[node];
}
node = n;
while (father[node] != 0) {
cp[father[node]][node] -= k;
node = father[node];
}
for (int i = 1; i <= n; i++)
viz[i] = 0;
s += k;
k = INF;
}
out << s;
}