Pagini recente » Cod sursa (job #2431402) | Cod sursa (job #1412996) | Cod sursa (job #2452686) | Cod sursa (job #557402) | Cod sursa (job #2284901)
#include <bits/stdc++.h>
using namespace std;
/// by iterating all neighbours of the sink
const int DIM_MAX = 1010;
namespace Flow {
int tata[DIM_MAX];
int c[DIM_MAX][DIM_MAX];
vector <int> adia[DIM_MAX];
int s, d, n;
bool bfs() {
memset(tata, 0, sizeof tata);
queue <int> q;
q.push(s);
tata[s] = 1;
while (!q.empty()) {
int nod = q.front();
q.pop();
if (nod == d)
continue;
for (auto i : adia[nod]) {
if (c[nod][i] && !tata[i]) {
tata[i] = nod;
q.push(i);
}
}
}
return tata[d];
}
int augment() {
int add = 1e9;
for (int i = d, l = 0; i != s; i = tata[i], l++) {
add = min(add, c[tata[i]][i]);
if (i == 0)
return 0;
}
for (int i = d; i != s; i = tata[i])
c[tata[i]][i] -= add, c[i][tata[i]] += add;
return add;
}
int max_flow(int _s, int _d, int _n) {
s = _s, d = _d, n = _n;
int flow = 0;
while (bfs()) {
for (int i = 1; i <= n; i++) {
if (c[i][d] && i != d) {
tata[d] = i;
flow += augment();
}
}
}
return flow;
}
}
int main()
{
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n, m, a, b, c;
in >> n >> m;
while (m--) {
in >> a >> b >> c;
Flow::c[a][b] += c;
Flow::adia[a].push_back(b);
Flow::adia[b].push_back(a);
}
out << Flow::max_flow(1, n, n) << '\n';
return 0;
}