Cod sursa(job #1878938)

Utilizator DobosDobos Paul Dobos Data 14 februarie 2017 16:59:28
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
#define NMAX 1010
#define INF 1e9

using namespace std;


ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");

vector < int > G[NMAX];

int F[NMAX][NMAX],ant[NMAX];
bool B[NMAX];
deque < int > Q;
int BFS(int n){
    int nod;
    for(int i = 2; i <= n; i++){
        B[i] = 0; ant[i] = 0;
    }
    Q.push_back(1);
    while(!Q.empty()){
        nod = Q.front();
        Q.pop_front();
        for(auto const &it : G[nod]){
            if(B[it] == 0 && F[nod][it] > 0){

                B[it] = 1;
                Q.push_back(it);
                ant[it] = nod;

            }

        }
    }

    return B[n];
}

int main()
{
    ios :: sync_with_stdio(false);
    fin.tie(NULL);

    int n,m,x,y,c;
    fin >> n >> m;
    for(int i = 1; i <= m; i++){
        fin >> x >> y >> c;
        G[x].push_back(y);
        G[y].push_back(x);
        F[x][y] = c;
    }

    int sol = 0, mflux;
    ant[1] = 1; B[1] = 1;
    while(BFS(n)){
        for(auto const &it : G[n]){
           // if(B[it] == 1 && F[it][n] > 0){
                ant[n] = it;
                mflux = INF;

                for(int j = n; j != 1; j = ant[j])
                    mflux = min(mflux,F[ant[j]][j]);

                for(int j = n; j != 1; j = ant[j]){

                    F[ant[j]][j] -= mflux;
                    F[j][ant[j]] += mflux;
                }

                sol += mflux;

           // }
        }
    }

    fout << sol;

    return 0;
}