Pagini recente » Cod sursa (job #2645691) | Cod sursa (job #396904) | Cod sursa (job #1169535) | Cod sursa (job #2217615) | Cod sursa (job #2424042)
#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 = 0; 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);
}
}
}
return used[n];
}
int viz[1003];
void dfs(int s)
{
viz[s] = 1;
for (auto elem : v[s]) {
if (viz[elem.node] == 0 and viz[elem.cost] > 0) {
if (elem.node == n)
g << "bun";
dfs(elem.node);
}
}
}
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;
bool ok = false;
for (int j = 0; j < v[x].size(); j++) {
if (v[x][j].node == y) {
v[x][j].cost += c;
ok = true;
break;
}
}
if(ok==false)
v[x].push_back(a);
}
int max_flow = 0;
int count = 0;
while (bfs() == 1) {
int flow_act = cost[n];
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 << '\n';
//g<<bfs();
return 0;
}