Pagini recente » Cod sursa (job #2827201) | Cod sursa (job #909623) | Cod sursa (job #2520389) | Cod sursa (job #1865394) | Cod sursa (job #2421987)
#include <iostream>
#include <fstream>
#include <queue>
#include <bitset>
#include <vector>
/// drumuri de augmentare
/// Ford Fulkerson algorithm
/// cuplaj maxim in graf bipartit
/// problema harta infoarena
using namespace std;
#define NMAX 1010
ifstream f ("maxflow.in");
ofstream t ("maxflow.out");
int main() {
int C[NMAX][NMAX], n, m, total_flow = 0, parent[NMAX];
bitset<NMAX> vaz;
vector<int> v[NMAX];
auto bfs = [&vaz, &v, &C, &parent](int start, int finish) {
queue<int> q;
vaz[start] = true;
q.push(start);
while (!q.empty()) {
int current = q.front();
q.pop();
cout << current << std::endl;
for (auto i : v[current]) {
cout << vaz[i] << " " << C[current][i] << endl;
if ((!vaz[i]) && C[current][i] > 0) {
vaz[i] = true;
parent[i] = current;
q.push(i);
}
}
}
return vaz[finish];
};
f >> n >> m;
total_flow &= 0;
for (int i = 0, x , y, c; i < m; ++i) {
f >> x >> y >> c;
C[x][y] = c;
C[y][x] = 0;
v[x].push_back(y);
v[y].push_back(x);
}
for (int minflow; bfs(1, n); vaz.reset()) {
for(auto i : v[n]) {
if (!C[i][n] || !vaz[i])
continue;
parent[n] = i;
minflow = 1e8;
for (int nod = n; nod != 1; nod = parent[nod])
minflow = C[parent[nod]][nod] < minflow ? C[parent[nod]][nod] : minflow;
if (minflow == 0)
continue;
for (int nod = n; nod != 1; nod = parent[nod])
C[parent[nod]][nod] -= minflow,
C[nod][parent[nod]] += minflow;
total_flow += minflow;
}
}
t << total_flow;
return 0;
}