Pagini recente » Cod sursa (job #56072) | Cod sursa (job #2161188) | Cod sursa (job #2815649) | Cod sursa (job #911593) | Cod sursa (job #1688164)
using namespace std;
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using pii = pair<int, int>;
#define Nmax 1001
int n, max_flow;
int min_f[Nmax], father[Nmax], f[Nmax][Nmax];
queue< int > Q;
vector< pii > G[Nmax];
vector< bool > vis;
bool BFS(int);
void read();
void write();
int main()
{
int cf, vf;
read();
while (BFS(1))
{
for (auto &to : G[n])
if(min_f[to.first] > 0)
{
cf = min_f[to.first];
for (vf = to.first; father[vf] != 0; vf = father[vf])
f[father[vf]][vf] += cf,
f[vf][father[vf]] -= cf;
max_flow += cf;
}
}
write();
return 0;
}
bool BFS(int vf)
{
vis.assign(n + 1, false);
fill(min_f + 1, min_f + n + 1, 0);
vis[vf] = true;
father[vf] = 0;
for (Q.push(vf); !Q.empty(); Q.pop())
{
vf = Q.front();
if (vf == n) continue;
for(auto &to : G[vf])
if (!vis[to.first] && f[vf][to.first] < to.second)
{
if (min_f[vf] == 0) min_f[to.first] = to.second - f[vf][to.first];
else min_f[to.first] = min(to.second - f[vf][to.first], min_f[vf]);
father[to.first] = vf;
vis[to.first] = true;
Q.push(to.first);
}
}
return vis[n];
}
void read()
{
int m, a, b, c;
ifstream fin("maxflow.in");
fin >> n;
vis.resize(n + 1);
for (fin >> m; m; --m)
{
fin >> a >> b >> c;
G[a].emplace_back(b, c);
G[b].emplace_back(a, 0);
}
fin.close();
}
void write()
{
ofstream fout("maxflow.out");
fout << max_flow << '\n';
fout.close();
}