Pagini recente » Cod sursa (job #1114645) | Cod sursa (job #2968960) | Cod sursa (job #931556) | Cod sursa (job #3289944) | Cod sursa (job #3221314)
#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());
struct MaxFlow
{
#define N 5000
int n , m , sink , source;
int ptr[N] , lvl[N];
vector < int > e[N];
queue < int > q;
struct edge
{
int x , y , cap;
} edg[N];
void init(int n , int source , int sink)
{
this -> n = n;
this -> sink = sink;
this -> source = source;
m = 0;
}
void addEdge(int x , int y , int cap)
{
e[x].pb(m);
edg[m++] = {x , y , cap};
e[y].pb(m);
edg[m++] = {y , x , 0};
}
bool bfs()
{
for(int i = 1 ; i <= n ; i++)
lvl[i] = 0;
lvl[source] = 1;
q.push(source);
while(q.size())
{
int node = q.front();
q.pop();
for(auto E : e[node])
{
int to = edg[E].x ^ edg[E].y ^ node;
if(!lvl[to] && edg[E].cap)
{
lvl[to] = lvl[node] + 1;
q.push(to);
}
}
}
return lvl[sink] != 0;
}
int dfs(int node , int pushedFlow)
{
if(node == sink)
return pushedFlow;
for(int &i = ptr[node] ; i < e[node].size() ; i++)
{
int E = e[node][i];
int to = edg[E].x ^ edg[E].y ^ node;
int cap = edg[E].cap;
if(lvl[node] + 1 == lvl[to] && cap)
{
int f = dfs(to , min(pushedFlow , cap));
if(f == -1) continue;
edg[E].cap -= f;
edg[E ^ 1].cap += f;
return f;
}
}
return -1;
}
int flow()
{
int F = 0;
while(bfs())
{
for(int i = 1 ; i <= n ; i++)
ptr[i] = 0;
int f;
while((f = dfs(source , INT_MAX)) != -1)
F += f;
}
return F;
}
#undef N
} F;
signed main()
{
#ifndef ONLINE_JUDGE
FastIO , FILES;
#endif
int n , m;
cin >> n >> m;
F.init(n , 1 , n);
while(m--)
{
int x , y , z;
cin >> x >> y >> z;
F.addEdge(x , y , z);
}
cout << F.flow();
return 0;
}