Pagini recente » Cod sursa (job #1077573) | Cod sursa (job #1393848) | Cod sursa (job #2792988) | Cod sursa (job #2365932) | Cod sursa (job #2760536)
#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 < int , int >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'
namespace FastRead
{
char __buff[5000];int __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 int N = 1e3 + 10;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n , m , x , y , c;
int capacity[N][N] , parent[N];
vector < int > G[N];
queue < pii > q;
int bfs(int source , int sink)
{
for(int i = 1 ; i <= n ; i++)
parent[i] = -1;
parent[source] = -2;
q = queue < pii > ();
q.push({source , INT_MAX});
while(!q.empty())
{
int cur = q.front().first;
int flow = q.front().second;
q.pop();
for(auto go : G[cur])
if(parent[go] == -1 && capacity[cur][go])
{
parent[go] = cur;
int new_flow = min(flow , capacity[cur][go]);
q.push({go , new_flow});
if(go == sink)
return new_flow;
}
}
return 0;
}
int max_flow(int source , int sink)
{
int flow = 0;
while(int new_flow = bfs(source , sink))
{
int cur = sink;
flow += new_flow;
while(cur != source)
{
int prev = parent[cur];
capacity[prev][cur] -= new_flow;
capacity[cur][prev] += new_flow;
cur = prev;
}
}
return flow;
}
signed main()
{
#ifndef ONLINE_JUDGE
FastIO , FILES;
#endif
cin >> n >> m;
for(int i = 1 ; i <= m ; i++)
{
cin >> x >> y >> c;
capacity[x][y] = c;
G[x].pb(y);
G[y].pb(x);
}
cout << max_flow(1 , n);
return 0;
}