Pagini recente » Cod sursa (job #2881122) | Cod sursa (job #860037) | Cod sursa (job #669668) | Cod sursa (job #2601351) | Cod sursa (job #2806152)
#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;
void getPath()
{
memset(parent, 0, sizeof(parent));
vector<int> q;
parent[s] = -1;
q.push_back(s);
for (int i = 0; i < q.size() ; i++)
{
if (q[i] == n)
{
continue;
}
for (auto to: la[q[i]])
{
int flx_left = fluxLeft[q[i]][to];
if (flx_left <= 0)
continue;
if (parent[to] != 0)
continue;
// cout << q.front() << ' ' << to << '\n';
parent[to] = q[i];
q.push_back(to);
// daca am ajuns la final, construim path si o returnam
if (to == f)
{
return;
}
}
}
}
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;
}
getPath();
while (parent[f] != 0)
{
# 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;
int nod = f;
while (parent[nod] != -1)
{
if (fluxLeft[parent[nod]][nod] < minn)
minn = fluxLeft[parent[nod]][nod];
nod = parent[nod];
}
flux += minn;
nod = f;
while (parent[nod] != -1)
{
fluxLeft[parent[nod]][nod] -= minn;
fluxLeft[nod][parent[nod]] += minn;
nod = parent[nod];
}
# if 0
nod = f;
while (parent[nod] != -1)
{
cout << parent[nod] << ' ' << nod << ' ' << fluxLeft[nod][parent[nod]] << '\n';
nod = parent[nod];
}
cout << "\n\n\n";
# endif
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
*/