Cod sursa(job #2790314)

Utilizator tigaieNedelcu Ioan-Andrei tigaie Data 28 octombrie 2021 19:35:51
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.79 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 1001
vector <int> adj[nax];
int flow[nax][nax] , capacity[nax][nax];
int depth[nax];
int n, m, x, y, w;

bool bfs() {
    queue <int> q;
    memset(depth, 0, sizeof(depth));
    depth[1] = 1;
    q.push(1);
    while(!q.empty()) {
        int nod = q.front();
        q.pop();
        for(int &v : adj[nod]) {
            if(depth[v] == 0 && capacity[nod][v] > flow[nod][v]) {
                depth[v] = depth[nod] + 1;
                q.push(v);
            }
        }
    }
    return (depth[n] != 0);
}

int dfs(int nod, int fflow) {
    if(nod == n)
        return fflow;

    for(int &v : adj[nod]) {
        if(depth[v] == depth[nod] + 1 && capacity[nod][v] - flow[nod][v] > 0) {
            int tmpFlow = dfs(v, min(fflow, capacity[nod][v] - flow[nod][v]));
            if(tmpFlow > 0) {
                flow[nod][v] += tmpFlow;
                flow[v][nod] -= tmpFlow;
                return tmpFlow;
            }
        }
    }
    depth[nod] = 0;
    return 0;
}

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;
    for(int i = 0 ; i < m ; i++) {
        cin >> x >> y >> w;
        adj[x].push_back(y);
        adj[y].push_back(x);
        capacity[x][y] = w;
    }
    int maxFlow = 0;
    while(bfs()) {
        int tempFlow = 0;
        do {
            tempFlow = dfs(1 , inf);
            maxFlow += tempFlow;
        }while(tempFlow > 0);
    }

    cout << maxFlow << 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;
}