Pagini recente » Cod sursa (job #1981722) | Cod sursa (job #642834) | Cod sursa (job #919611) | Cod sursa (job #1671055) | Cod sursa (job #2806113)
#include <iostream>
#include <bits/stdc++.h>
#define maxFlow 110000
using namespace std;
ifstream in ("maxflow.in");
ofstream out ("maxflow.out");
int n, m;
int s, f;
vector<int> la[1001];
int fluxLeft[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()])
{
int flx_left = fluxLeft[q.front()][to];
if (flx_left <= 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);
fluxLeft[x][y] = c;
}
vector<pair<int,int>> currentPath = getPath();
while (currentPath.size())
{
# if 0
for (auto per: currentPath)
cout << per.first << ' ' << per.second << ' ' << fluxLeft[per.first][per.second].fluxLeft << '\n';
cout << "\n\n";
# endif
int minn = maxFlow;
for (auto& per: currentPath)
{
int flx_left = fluxLeft[per.first][per.second];
if (flx_left < minn)
minn = flx_left;
}
flux += minn;
for (auto& per: currentPath)
{
// actualizeaza si muchia intoarsa
int& flx_left = fluxLeft[per.first][per.second];
flx_left -= minn;
int& flx_left_rev = fluxLeft[per.second][per.first];
flx_left_rev += minn;
}
# if 0
for (auto& per: currentPath)
{
info inf = fluxLeft[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;
}
/*
6 8
1 2 10
1 3 8
2 3 2
2 4 5
3 5 10
5 4 8
4 6 7
5 6 10
// 15
*/
/*
6 7
1 2 6
1 3 8
2 4 5
3 5 4
2 5 3
4 6 7
5 6 5
// 10
*/