Pagini recente » Cod sursa (job #2695228) | Cod sursa (job #2499062) | Cod sursa (job #2496916) | Cod sursa (job #2755529) | Cod sursa (job #2496838)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int N = 1009;
namespace Flow {
struct Edge {
int from;
int to;
int flow;
};
int S, D, n;
vector < Edge > muchii;
vector < int > adia[N];
int tata[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 edge : adia[nod]) {
auto &a = muchii[edge];
if (tata[a.to] == -1 && a.flow)
tata[a.to] = edge, q.push_back(a.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 flow(muchii[i].flow);
for (int nod = D; nod != S; nod = muchii[tata[nod]].from)
flow = min(flow, muchii[tata[nod]].flow);
for (int nod = D; nod != S; nod = muchii[tata[nod]].from)
muchii[tata[nod]].flow -= flow,
muchii[tata[nod] ^ 1].flow += flow;
ans += flow;
}
return ans;
}
int FluxMaxim (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;
Flow::AddEdge(a, b, c);
}
fout << Flow::FluxMaxim(1, n, n);
return 0;
}