Pagini recente » Cod sursa (job #1812327) | Cod sursa (job #2478644) | Cod sursa (job #796478) | Cod sursa (job #601802) | Cod sursa (job #3189201)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, maxFlow = 0, m;
int viz[1005];
int aug_path[1005];
int last[1005];
struct elem
{
int node, cap, flow;
};
vector<elem>V[1005];
pair<int, int>rezidual[1005][1005];
bool BFS(int node)
{
for(int i = 1;i <= n;i++)
last[i] = viz[i] = aug_path[i] = 0;
queue<int>q;
q.push(node);
viz[node] = 1;
while(!q.empty())
{
int nod = q.front();
q.pop();
for(auto x : V[nod])
if(viz[x.node] == 0 && (rezidual[nod][x.node].first - rezidual[nod][x.node].second) > 0 )
{
viz[x.node] = 1;
last[x.node] = nod;
q.push(x.node);
}
}
int nod = n, cnt = 0;
aug_path[++cnt] = nod;
while(last[nod] != 0)
{
//cout << nod << " ";
aug_path[++cnt] = last[nod];
nod = last[nod];
}
if(cnt <= 1)
return false;
reverse(aug_path + 1, aug_path + cnt + 1);
int bottleneck = 1e9;
for(int i = 1;i < cnt;i++)
{
int x = aug_path[i];
int y = aug_path[i + 1];
bottleneck = min(bottleneck, rezidual[x][y].first - rezidual[x][y].second);
}
for(int i = 1;i < cnt;i++)
{
int x = aug_path[i];
int y = aug_path[i + 1];
rezidual[x][y].second += bottleneck;
rezidual[y][x].second -= bottleneck;
}
maxFlow += bottleneck;
return true;
}
void Solve()
{
fin >> n >> m;
for(int i = 1;i <= m;i++)
{
int x, y, c;
fin >> x >> y >> c;
V[x].push_back({y, c, 0});
V[y].push_back({x, 0, 0});
rezidual[x][y] = {c, 0};
rezidual[y][x] = {0, 0};
}
while(BFS(1))
;
fout << maxFlow << "\n";
}
int main()
{
Solve();
return 0;
}