Pagini recente » Cod sursa (job #1153505) | Cod sursa (job #2667169) | Cod sursa (job #2544036) | Cod sursa (job #1335775) | Cod sursa (job #2886222)
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("maxflow.in" , "r" , stdin) , freopen("maxflow.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < ll , ll >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'
namespace FastRead
{
char __buff[5000];ll __lg = 0 , __p = 0;
char nc()
{
if(__lg == __p){__lg = fread(__buff , 1 , 5000 , stdin);__p = 0;if(!__lg) return EOF;}
return __buff[__p++];
}
template<class T>void read(T&__x)
{
T __sgn = 1; char __c;while(!isdigit(__c = nc()))if(__c == '-')__sgn = -1;
__x = __c - '0';while(isdigit(__c = nc()))__x = __x * 10 + __c - '0';__x *= __sgn;
}
}
using namespace FastRead;
using namespace std;
const ll N = 1e5 + 10;
const ll M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll n , m , x , y , c;
struct Edge {ll x , y , c;};
vector < ll > G[N];
vector < Edge > edges;
ll ptr[N] , lev[N];
ll source , sink;
queue < ll > q;
ll sz = 0;
void addEdge(ll x , ll y , ll c)
{
edges.pb({x , y , c});
edges.pb({y , x , 0});
G[x].pb(sz);
G[y].pb(sz + 1);
sz += 2;
}
bool bfs()
{
q.push(source);
lev[source] = 1;
while(!q.empty())
{
ll node = q.front(); q.pop();
for(auto e : G[node])
{
ll to = edges[e].y;
if(lev[to] == -1 && edges[e].c)
lev[to] = lev[node] + 1 , q.push(to);
}
}
return lev[sink] != -1;
}
ll dfs(ll node , ll pushed_flow)
{
if(node == sink) return pushed_flow;
for(ll &pt = ptr[node] ; pt < G[node].size() ; pt++)
{
ll e = G[node][pt];
ll to = edges[e].y;
if(lev[to] == lev[node] + 1 && edges[e].c)
{
ll pushed = dfs(to , min(edges[e].c , pushed_flow));
if(pushed == 0) continue;
edges[e].c -= pushed;
edges[e ^ 1].c += pushed;
return pushed;
}
}
return 0;
}
signed main()
{
#ifndef ONLINE_JUDGE
FastIO , FILES;
#endif
cin >> n >> m;
for(ll i = 1 ; i <= m ; i++)
{
cin >> x >> y >> c;
addEdge(x , y , c);
}
source = 1;
sink = n;
ll f = 0;
while(1)
{
for(ll i = 1 ; i <= n ; i++)
lev[i] = -1 , ptr[i] = 0;
if(!bfs())
break;
while(ll flow = dfs(source , INT_MAX))
f += flow;
}
cout << f;
return 0;
}