Pagini recente » Cod sursa (job #1214367) | Cod sursa (job #2914339) | Cod sursa (job #2385414) | Cod sursa (job #426828) | Cod sursa (job #2428513)
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
#define nmax 1005
#define inf 2000000000
int n, m;
int mat[nmax][nmax];
int flowGraph[nmax][nmax];
int used[nmax];
int p[nmax];
void init() {
for (int i = 1; i <= n; i++) {
p[i] = used[i] = 0;
}
}
void dfs(int rad) {
used[rad] = 1;
for (int i = 1; i <= n; i++) {
if (used[i] == 0 and mat[rad][i] > flowGraph[rad][i]) {
p[i] = rad;
dfs(i);
}
}
}
int main() {
f >> n >> m;
int x, y, c;
for (int i = 1; i <= m; i++) {
f >> x >> y >> c;
mat[x][y] += c;
}
int total = 0;
init();
dfs(1);
while (used[n]) {
int maxFlow = inf,cn=n;
while (p[cn] != 0) {
maxFlow = min(maxFlow, mat[p[cn]][cn] - flowGraph[p[cn]][cn]);
cn = p[cn];
}
cn = n;
while (p[cn] != 0) {
flowGraph[p[cn]][cn] += maxFlow;
flowGraph[cn][p[cn]] -= maxFlow;
cn = p[cn];
}
total += maxFlow;
init();
dfs(1);
}
g << total;
return 0;
}