Pagini recente » Cod sursa (job #617893) | Cod sursa (job #1311358) | Cod sursa (job #2246785) | Cod sursa (job #1395221) | Cod sursa (job #2806106)
#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};
}
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;
inf.fluxLeft -= minn;
info& inf_rev = lai[per.second][per.first];
// inf_rev.currentUsed -= minn;
inf_rev.fluxLeft += minn;
// inf_rev.fluxLeft -= minn;
}
# 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;
}
/*
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
*/