Pagini recente » Cod sursa (job #767711) | Cod sursa (job #2120964) | Cod sursa (job #3179417) | Cod sursa (job #2127336) | Cod sursa (job #2497538)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int N = 1009;
namespace Flux {
struct Edge {
int from, to, flow;
};
vector < Edge > muchii;
int tata[N];
vector < int > adia[N];
int S, D, n;
void AddEdge(int from, int to, int flow) {
adia[from].push_back(muchii.size());
muchii.push_back({from, to, flow});
adia[to].push_back(muchii.size());
muchii.push_back({to, from, 0});
}
bool bfs() {
fill(tata + 1, tata + n + 1, -1);
vector < int > q;
q.push_back(S);
for (int i = 0; i < q.size(); ++i) {
int nod = q[i];
if (nod == D)
continue;
for (int next : adia[nod]) {
auto &x = muchii[next];
if (tata[x.to] == -1 && x.flow)
tata[x.to] = next, q.push_back(x.to);
}
}
return (tata[D] != -1);
}
int push() {
int ans(0);
for (int i : adia[D]) {
i ^= 1;
if (muchii[i].flow == 0)
continue;
tata[D] = i;
int pv(muchii[i].flow);
for (int nod = D; nod != S; nod = muchii[tata[nod]].from)
pv = min(pv, muchii[tata[nod]].flow);
for (int nod = D; nod != S; nod = muchii[tata[nod]].from)
muchii[tata[nod]].flow -= pv,
muchii[tata[nod] ^ 1].flow += pv;
ans += pv;
}
return ans;
}
int MaxFlow(int s, int d, int _n) {
S = s;
D = d;
n = _n;
int ans(0);
while (bfs())
ans += push();
return ans;
}
}
int main()
{
int n, m, a, b, c;
fin >> n >> m;
while (m--) {
fin >> a >> b >> c;
Flux::AddEdge(a, b, c);
}
fout << Flux::MaxFlow(1, n, n);
return 0;
}