Pagini recente » Cod sursa (job #2253623) | Cod sursa (job #3226378)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("O1")
#pragma GCC optimize ("O2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree <long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update>
#define lsb(x)(x & (-x))
const int max_size = 1e3 + 20, INF = 2e9 + 2;
vector <int> mc[max_size];
int cap[max_size][max_size], t[max_size], viz[max_size], n;
bool bfs ()
{
for (int i = 1; i <= n; i++)
{
viz[i] = 0;
t[i] = 0;
}
viz[1] = 1;
queue <int> q;
q.push(1);
while (!q.empty())
{
int nod = q.front();
q.pop();
for (auto f : mc[nod])
{
if (viz[f] == 0 && cap[nod][f] > 0)
{
viz[f] = 1;
t[f] = nod;
q.push(f);
if (f == n)
{
return true;
}
}
}
}
return false;
}
void solve ()
{
int m;
cin >> n >> m;
while (m--)
{
int x, y, c;
cin >> x >> y >> c;
mc[x].push_back(y);
mc[y].push_back(x);
cap[x][y] += c;
}
int flux = 0;
while (bfs())
{
for (auto f : mc[n])
{
if (viz[f] == 0 || cap[f][n] <= 0)
{
continue;
}
int nod = n, mn = INF;
while (nod != 1)
{
mn = min(mn, cap[t[nod]][nod]);
nod = t[nod];
}
flux += mn;
if (mn == 0)
{
continue;
}
nod = n;
while (nod != 1)
{
cap[t[nod]][nod] -= mn;
cap[nod][t[nod]] += mn;
nod = t[nod];
}
}
}
cout << flux;
cout << '\n';
}
signed main ()
{
#ifdef LOCAL
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#else
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
#endif // LOCAL
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long tt;
//cin >> tt;
tt = 1;
while (tt--)
{
solve();
}
return 0;
}