Pagini recente » Cod sursa (job #1386212) | Cod sursa (job #69862) | Cod sursa (job #366782) | Cod sursa (job #586562) | Cod sursa (job #1688236)
using namespace std;
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
using pii = pair<int, int>;
#define INF 0x3f3f3f3f
#define Nmax 1001
int n;
int father[Nmax];
int f[Nmax][Nmax], c[Nmax][Nmax];
queue< int > Q;
vector< int > G[Nmax];
vector< bool > vis;
bool BFS(int);
void read();
void write(int);
int main()
{
int cf, vf, max_flow = 0;
read();
while (BFS(1))
{
for (auto &to : G[n])
if(vis[to] && f[to][n] < c[to][n])
{
father[n] = to;
for (cf = -1, vf = n; father[vf] != 0; vf = father[vf])
if (cf == -1 || c[father[vf]][vf] - f[father[vf]][vf] < cf)
cf = c[father[vf]][vf] - f[father[vf]][vf];
if(cf > 0)
for (vf = n; father[vf] != 0; vf = father[vf])
{
f[father[vf]][vf] += cf;
f[vf][father[vf]] -= cf;
}
max_flow += cf;
}
}
write(max_flow);
return 0;
}
bool BFS(int vf)
{
vis.assign(n + 1, false);
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] && f[vf][to] < c[vf][to])
{
father[to] = vf;
vis[to] = true;
Q.push(to);
}
}
return vis[n];
}
void read()
{
int m, x, y, z;
ifstream fin("maxflow.in");
fin >> n;
vis.resize(n + 1);
for (fin >> m; m; --m)
{
fin >> x >> y >> z;
G[x].push_back(y);
G[y].push_back(x);
c[x][y] += z;
}
fin.close();
}
void write(int ans)
{
ofstream fout("maxflow.out");
fout << ans << '\n';
fout.close();
}