Cod sursa(job #2588559)

Utilizator Senth30Denis-Florin Cringanu Senth30 Data 24 martie 2020 22:26:37
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.47 kb
#include <bits/stdc++.h>

using namespace std;
const int NMAX = 1010;
int N, M, flux, add_flux;
int father[NMAX], flow[NMAX];
int cap[NMAX][NMAX];

vector <int> edges[NMAX];
queue <int> Q;

void read(){
    scanf("%d%d", &N, &M);
    int a, b, c;
    for(int i = 1; i <= M; i++){
        scanf("%d%d%d", &a, &b, &c);
        edges[a].push_back(b);
        edges[b].push_back(a);
        cap[a][b] = c;;
    }
    add_flux = 1;
}

void bellman_ford(){
    Q.push(1);
    flow[1] = 2e9;
    while(!Q.empty()){
        int node = Q.front();
        Q.pop();
        for(int i = 0; i < edges[node].size(); i++){
            int x = edges[node][i];
            if(!cap[node][x]) continue;
            if(flow[x] < min(flow[node], cap[node][x])){
                flow[x] = min(flow[node], cap[node][x]);
                father[x] = node;
                Q.push(x);
            }
        }
    }
    add_flux = flow[N];
    int node = N;
    while(father[node] != 0){
        cap[father[node]][node] -= add_flux;
        cap[node][father[node]] += add_flux;
        node = father[node];
    }
}

int main(){

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

    read();

    while(add_flux){
        add_flux = 0;
        for(int i = 1; i <= N; i++){
            father[i] = 0;
            flow[i] = 0;
        }
        bellman_ford();
        flux += add_flux;
    }
    printf("%d", flux);

    return 0;
}