Cod sursa(job #2790301)

Utilizator tigaieNedelcu Ioan-Andrei tigaie Data 28 octombrie 2021 19:15:49
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.88 kb
/*
  (づ  ̄ ³ ̄)づ
*/
///default_boilerplate

#include <iostream>
#include <type_traits>
#include <iterator>
#include <utility>
#include <string>
namespace detail{
    template < typename T > struct has_begin{
        template < typename U > static std::true_type test( U&, decltype( begin( std::declval<U&>() ) )* ) ;
        template < typename U > static std::false_type test( U&, ... ) ;
        using type = decltype( test( std::declval<T&>(), nullptr ) ) ;
        static constexpr bool value = type::value ;
    };
    template < typename T > typename std::enable_if< has_begin<T>::value, std::ostream& >::type
    print( std::ostream& stm, const T& seq ) ;
    template < typename T > typename std::enable_if< !has_begin<T>::value, std::ostream& >::type
    print( std::ostream& stm, const T& v ) ;
    std::ostream& print( std::ostream& stm, const std::string& str ) { return stm << '"' << str << '"' ;}
    template < typename F, typename S > std::ostream& print( std::ostream& stm, const std::pair<F,S>& pair )
    { return print( print( stm << "{ ", pair.first ) << ", ", pair.second ) << " }" ; }
    template < typename T > typename std::enable_if< has_begin<T>::value, std::ostream& >::type
    print( std::ostream& stm, const T& seq ){
        stm << "[ " ;
        for( const auto& v : seq ) print( stm, v ) << ' ' ;
        return stm << ']' ;
    }
    template < typename T > typename std::enable_if< !has_begin<T>::value, std::ostream& >::type
    print( std::ostream& stm, const T& v ) { return stm << v ; }
}
template < typename T > std::ostream& print( const T& v, std::ostream& stm = std::cout )
{ return detail::print(stm,v) ; }

#define NDEBUG /// enable assert
//#define _DEBUG
#define _FILES
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;using namespace std;using ll = long long;
using ull = unsigned long long;using point = complex<int>;using pii = pair<int , int>;
template <typename T>using pair2 = pair<T , T>;
template <typename T>using min_queue = priority_queue< T , vector<T> , greater<T>>;
const int inf = int(1e9) , dx[4] = {1 , 0 , -1 , 0} , dy[4] = {0 , 1 , 0 , -1};
const ll inf_64 = ll(1e18);
const long double eps = 1e-9;
#define FOR(i , n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define sort_up(x) sort(all(x))
#define sort_down(x) sort(rall(x))
#define re real()
#define im imag()
#define endl "\n"
////===========================================================================================================
#define nax 10000
int n , m , s , t;
vector<pair<int , int>> edges;

vector<vector<int>> capacity(nax , vector<int>(nax , 0));
vector<vector<int>> adj(nax , vector<int>(0));

int bfs(int s, int t, vector<int>& parent) {
    fill(parent.begin(), parent.end(), -1);
    parent[s] = -2;
    queue<pair<int, int>> q;
    q.push({s, inf});

    while (!q.empty()) {
        int cur = q.front().first;
        int flow = q.front().second;
        q.pop();

        for (int next : adj[cur]) {
            if (parent[next] == -1 && capacity[cur][next]) {
                parent[next] = cur;
                int new_flow = min(flow, capacity[cur][next]);
                if (next == t)
                    return new_flow;
                q.push({next, new_flow});
            }
        }
    }

    return 0;
}

int maxflow(int s, int t) {
    int flow = 0;
    vector<int> parent(n + 1);
    int new_flow;

    while (new_flow = bfs(s, t, parent)) {
        flow += new_flow;
        int cur = t;
        while (cur != s) {
            int prev = parent[cur];
            capacity[prev][cur] -= new_flow;
            capacity[cur][prev] += new_flow;
            cur = prev;
        }
    }
    return flow;
}

signed main() {
#ifdef _DEBUG
    freopen("input.txt" , "r" , stdin);
    freopen("output.txt" , "w" , stdout);
    int t = clock();
#endif
#ifdef _FILES
    freopen("maxflow.in" , "r" , stdin);
    freopen("maxflow.out" , "w" , stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout << fixed << setprecision(15);
///code for the problem
///===============================================================
    cin >> n >> m;
    s = 1, t = n;
    for(int i = 0 ; i < m ; i++) {
        int a , b , w;
        cin >> a >> b >> w;
        adj[a].push_back(b);
        capacity[a][b] = w;
    }
    int max_flow_cal = maxflow(s , t);
    cout << max_flow_cal << endl;
///===============================================================
///debugging
#ifdef _DEBUG
    cerr << "Time = " << clock() - t << "ms" << endl;
    std::time_t current_time = std::time(0);
    std::tm* now = std::localtime(&current_time);
    cerr << "Date = " << (now -> tm_year + 1900) << " : " << (now -> tm_mon + 1) << " : " << (now -> tm_mday) << endl;
    cerr << "Hour = " << (now -> tm_hour) << ":" << (now -> tm_min) << ":" << (now -> tm_sec)<< endl;
#endif
    return 0x0;
}