Cod sursa(job #1882532)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 17 februarie 2017 12:04:28
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("maxflow.in");
ofstream g("maxflow.out");

constexpr int maxn = 1e3 + 10;
int n, m, s, t, tata[maxn], cap[maxn][maxn], flux[maxn][maxn];
vector<int> vec[maxn];

int calc_flux(){
    int rez = 0;
    while(true){
        memset(tata, 0x3f, sizeof(tata));
        queue<int, list<int>> de_viz;
        de_viz.push(s);
        while(!de_viz.empty()){
            const int cur = de_viz.front();
            de_viz.pop();
            for(const auto next : vec[cur]){
                if(cap[cur][next] == flux[cur][next] || tata[next] <= n) continue;
                tata[next] = cur;
                if(next == t) continue;
                de_viz.push(next); } }
        if(tata[t] > n) return rez;
        for(const auto frunza : vec[t]){
            if(cap[frunza][t] == flux[frunza][t] || tata[frunza] > n) continue;
            tata[t] = frunza;
            int next_flux = 1e9;
            for(int i = t; i != s; i = tata[i])
                next_flux = min(next_flux, cap[tata[i]][i] - flux[tata[i]][i]);
            rez += next_flux;
            for(int i =t; i != s; i = tata[i])
                flux[tata[i]][i] += next_flux,
                flux[i][tata[i]] -= next_flux; } } }

int main()
{
    f >> n >> m;
    s = 0, t = n-1;
    for(int i = 0, x, y; i < m; ++i){
        f >> x >> y;
        --x, --y;
        f >> cap[x][y];
        vec[x].push_back(y);
        vec[y].push_back(x); }
    g << calc_flux() << endl;
    return 0; }