Pagini recente » Cod sursa (job #2534016) | Cod sursa (job #2172800) | Cod sursa (job #1192535) | Cod sursa (job #1325171) | Cod sursa (job #2806025)
#include <iostream>
#include <bits/stdc++.h>
#define maxFlow 110000
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
struct info
{
int flux;
int currentUsed;
int fluxLeft;
};
int n, m;
int s, f;
vector<int> la[1001];
info lai[1001][1001];
int parent[1001];
int flux = 0;
vector<pair<int,int>> getPath()
{
for (int i = 1; i <= n; i++)
parent[i] = 0;
queue<int> q;
parent[s] = -1;
q.push(s);
vector<pair<int,int>> path;
while (q.size())
{
for (auto to: la[q.front()])
{
info& inf = lai[q.front()][to];
if (inf.fluxLeft <= 0)
continue;
if (parent[to] != 0)
continue;
// cout << q.front() << ' ' << to << '\n';
if (parent[to] == 0)
{
parent[to] = q.front();
q.push(to);
}
// daca am ajuns la final, construim path si o returnam
if (to == f)
{
while (parent[to] != -1)
{
// cout << 1;
path.push_back({parent[to], to});
to = parent[to];
}
return path;
}
}
q.pop();
}
return path;
}
int main()
{
in >> n >> m;
s = 1; f = n;
for (int i = 1; i <= m ; ++i)
{
int x,y,c;
in >> x >> y >> c;
la[x].push_back(y);
la[y].push_back(x);
lai[x][y] = {c, 0, c};
lai[y][x] = {0, 0, 0};
}
vector<pair<int,int>> currentPath = getPath();
while (currentPath.size())
{
# if 0
for (auto per: currentPath)
cout << per.first << ' ' << per.second << ' ' << lai[per.first][per.second].fluxLeft << '\n';
cout << "\n\n";
# endif
int minn = maxFlow;
for (auto& per: currentPath)
{
info& inf = lai[per.first][per.second];
if (inf.fluxLeft < minn)
minn = inf.fluxLeft;
}
flux += minn;
for (auto& per: currentPath)
{
// actualizeaza si muchia intoarsa
info& inf = lai[per.first][per.second];
inf.currentUsed += minn;
inf.fluxLeft = inf.flux - inf.currentUsed;
info& inf_rev = lai[per.second][per.first];
inf_rev.currentUsed -= minn;
inf_rev.fluxLeft = inf_rev.flux - inf_rev.currentUsed;
}
# if 0
for (auto& per: currentPath)
{
info inf = lai[per.first][per.second];
cout << per.first << ' ' << per.second << " " << inf.currentUsed << " / " << inf.flux << " / " << inf.fluxLeft << '\n';
}
cout << "\n\n\n";
# endif
currentPath = getPath();
}
out << flux;
return 0;
}