Cod sursa(job #2334002)

Utilizator Cristian.BBurghelea Cristian Cristian.B Data 2 februarie 2019 10:18:13
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <bits/stdc++.h>

using namespace std;
#define MAXN 1000

int n, m, S, D, flux;
vector <int> G[1 + MAXN];
int C[1 + MAXN][1 + MAXN];
int father[1 + MAXN];
int q[1 + MAXN], p, u;

inline bool bfs(){
    memset(father, 0, sizeof(father));
    p = 0, u = 1;
    q[0] = S;
    while(p != u){
        int node = q[p++];
        if(node != D)
            for(auto y:G[node])
                if(!father[y] && C[node][y]) father[y] = node, q[u++] = y;
    }
    return father[D];
}

int main(){

    ifstream fi("maxflow.in");
    ofstream fo("maxflow.out");

    fi>>n>>m;
    for(int i = 1; i <= m; i++){
        int x, y, z;
        fi>>x>>y>>z;
        G[x].push_back(y);
        G[y].push_back(x);
        C[x][y] += z;
    }

    S = 1, D = n;
    while(bfs())
        for(auto y:G[D])
            if(father[y] && C[y][D]){
                father[D] = y;
                int flow = 1000000000;
                for(int i = D; i != S && flow; i = father[i])
                        flow = min(flow, C[father[i]][i]);
                if(flow) for(int i = D; i != S; i = father[i])
                        C[father[i]][i] -= flow, C[i][father[i]] += flow;
                flux += flow;
            }
    fo<<flux;

    fi.close(); fo.close();
    return 0;
}