Pagini recente » Cod sursa (job #1576539) | Cod sursa (job #5630) | Cod sursa (job #770703) | Cod sursa (job #3297573) | Cod sursa (job #1942879)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vb = vector<bool>;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int NMAX = 1001;
int n, m;
int f[NMAX][NMAX], c[NMAX][NMAX];
vector<int> v[NMAX];
int t[NMAX];
int viz[NMAX];
inline void read() {
fin >> n >> m;
int x, y, cap;
for (int i = 1; i <= m; ++i) {
fin >> x >> y >> cap;
if (find(v[x].begin(), v[x].end(), y) == v[x].end())
v[x].push_back(y);
if (find(v[y].begin(), v[y].end(), x) == v[y].end())
v[y].push_back(x);
c[x][y] += cap;
f[x][y] = 0;
c[y][x] = 0;
}
}
inline bool bfs() {
queue<int> q;
for (int i = 1; i <= n; ++i)
viz[i] = 0;
q.push(1);
viz[1] = 1;
int node;
while (q.size()) {
node = q.front();
q.pop();
if (node == n)
continue;
for (const int& other: v[node]) {
if (viz[other])
continue;
if (c[node][other] <= f[node][other])
continue;
viz[other] = 1;
t[other] = node;
q.push(other);
}
}
return viz[n];
}
inline void getMaxFlow() {
int minFlow, ansFlow = 0;
while (bfs()) {
for (const int& other: v[n]) {
if (!viz[other])
continue;
if (c[other][n] <= f[other][n])
continue;
minFlow = 2e9;
t[n] = other;
for (int node = n; node != 1; node = t[node])
minFlow = min(minFlow, c[t[node]][node] - f[t[node]][node]);
if (minFlow == 0)
continue;
ansFlow += minFlow;
for (int node = n; node != 1; node = t[node]) {
f[t[node]][node] += minFlow;
f[node][t[node]] -= minFlow;
}
}
}
fout << ansFlow;
}
int main()
{
read();
getMaxFlow();
fin.close();
fout.close();
return 0;
}