Cod sursa(job #2417364)

Utilizator alex2kamebossPuscasu Alexandru alex2kameboss Data 29 aprilie 2019 16:22:59
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.23 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#include <queue>

#define N 1005

using namespace std;

int n,m,x,y,fmax;
int c[N][N], f[N][N], t[N];
vector <int> g[N];
bitset <N> viz;
vector <int> q;

bool dfs(){
    q.clear();
    q.push_back(1);
    viz.reset();
    while(!q.empty()){
        x = q.front();
        q.erase(q.begin());
        for(int y : g[x]){
            if(c[x][y] == f[x][y] || viz[y])
                continue;
            q.push_back(y);
            viz[y]=1;
            t[y] = x;
            if(y==n)
                return 1;
        }
    }

    return 0;
}

int main()
{
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);

    scanf("%d%d", &n, &m);
    for(int i = 0; i < m; ++i){
        scanf("%d%d", &x,&y);
        scanf("%d", &c[x][y]);
        g[x].push_back(y);
        g[y].push_back(x);
    }

    while(dfs()){
        int fmin = 0x3f3f3f3f;
        y=n;
        while(y!=1){
            x=t[y];
            fmin=min(fmin,c[x][y]-f[x][y]);
            y=x;
        }
        fmax += fmin;
        y=n;
        while(y!=1){
            x=t[y];
            f[x][y]+=fmin;
            f[y][x]-=fmin;
            y=x;
        }
    }

    cout<<fmax;

    return 0;
}