Cod sursa(job #2031672)

Utilizator ajeccAjechiloae Eugen ajecc Data 3 octombrie 2017 17:54:02
Problema Flux maxim Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.71 kb
#include <bits/stdc++.h>
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
#ifdef _WIN32
#include <windows.h>
#define print(x) PRINT(x, #x)
template<typename T> inline const void PRINT(T VARIABLE, string NAME)
{
#ifndef ONLINE_JUDGE /// ONLINE_JUDGE IS DEFINED ON CODEFORCES
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, 10);
    cerr << NAME << " = " << VARIABLE;
    SetConsoleTextAttribute(hConsole, 7);
    cerr << '\n';
#endif
}
#else
#define print(x) 0
#endif
struct pair_hash
{
    template<typename T1, typename T2>
    size_t operator () (const pair<T1, T2> &p) const
    {
        auto h1 = hash<T1> {}(p.first);
        auto h2 = hash<T2> {}(p.second);
        return h1 ^ h2;
    }
};
typedef long long ll;
typedef unsigned long long ull;
const ll INFLL = 2 * (ll)1e18 + 100;
const int INFINT = 2 * (int)1e9 + 100;
//ifstream fin(".in");
//ofstream fout(".out");
void die()
{
    cout << "-1\n";
    exit(0);
}
const int NMAX = 1e3 + 5;
const int MOD = 1e9 + 7; /// careful here (7 or 9, 66.. etc)
const double PI = atan(1) * 4;
const double EPS = 1e-12;

int n, m;
V graf[NMAX];
int cap[NMAX][NMAX], flow[NMAX][NMAX], father[NMAX];

bool trecut[NMAX];
bool bfs()
{
    memset(trecut, 0, sizeof(trecut));
    queue<int> coada;
    coada.push(1);
    trecut[1] = 1;
    while(!coada.empty())
    {
        int nod = coada.front();
        coada.pop();

        for(auto i: graf[nod]) if(!trecut[i] && cap[nod][i] > flow[nod][i])
        {
            trecut[i] = 1;
            father[i] = nod;
            coada.push(i);
        }
    }
    return trecut[n];
}

int sol;
#define cin fin
#define cout fout
int main()
{
    ifstream fin("maxflow.in");
    ofstream fout("maxflow.out");
    //FASTIO;
    cin >> n >> m;
    for1(i, m)
    {
        int a, b;
        cin >> a >> b;
        graf[a].pb(b);
        graf[b].pb(a);
        cin >> cap[a][b];
    }

    while(bfs())
        for(auto i: graf[n])
        {
            int mmin = INFINT;

            for(int nod = i; nod != 1; nod = father[nod])
                mmin = min(mmin, cap[father[nod]][nod] - flow[father[nod]][nod]);

            sol += mmin;
            for(int nod = i; nod != 1; nod = father[nod])
            {
                flow[father[nod]][nod] += mmin;
                flow[nod][father[nod]] -= mmin;
            }
        }

    cout << sol;

    return 0;
}