Pagini recente » Cod sursa (job #1028999) | Cod sursa (job #1354812) | Cod sursa (job #2748390) | Cod sursa (job #2560723) | Cod sursa (job #2423282)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
struct nod {
int cost, node;
};
vector<nod> v[1002];
int n, m;
bool used[1002];
int father[1002];
int cost[1002];
int bfs() {
queue<int> q;
q.push(1);
for (int i = 1; i <= n; i++)
{
used[i] = 0; father[i] = 0; cost[i] = 0;
}
used[1] = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto elem : v[u]) {
if (used[elem.node] == 0 and elem.cost > 0) {
used[elem.node] = 1;
father[elem.node] = u;
cost[elem.node] = elem.cost;
q.push(elem.node);
}
}
}
if (used[n] == 1)
return 1;
return 0;
}
int main() {
int x, y, c;
f >> n >> m;
nod a;
for (int i = 1; i <= m; i++)
{
f >> x >> y >> c;
a.cost = c;
a.node = y;
v[x].push_back(a);
}
int max_flow = 0;
while (bfs() == 1) {
int flow_act = INT32_MAX;
int init;
for (init = n; init != 1; init = father[init]) {
flow_act = min(flow_act, cost[init]);
}
max_flow += flow_act;
for (init = n; init != 1; init = father[init]) {
int u = father[init];
for (int i = 0; i < v[u].size(); i++) {
if (v[u][i].node == init)
{
v[u][i].cost -= flow_act;
}
}
}
}
g << max_flow;
return 0;
}