Cod sursa(job #1878521)

Utilizator DobosDobos Paul Dobos Data 14 februarie 2017 11:15:26
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <bits/stdc++.h>
#define NMAX 1005
#define INF 1e9
using namespace std;

int F[NMAX][NMAX];
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");

vector < int > G[NMAX];

deque < int > Q;
int ant[NMAX];
bool B[NMAX];
int BFS(int n){
    int nod;
    ant[1] = 1;
    memset (B,0,sizeof(B));
    B[1] = 1;
    Q.push_back(1);
    while(!Q.empty()){
        nod = Q.front();
        Q.pop_front();
        for(auto const &i : G[nod]){
            if(!B[i] && F[nod][i] > 0){
                Q.push_back(i);
                B[i] = 1;
                ant[i] = 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,mflow;
    while(BFS(n)){

        for(auto const &i : G[n]){
            if(F[i][n] != 0 || B[i] == 1){
                mflow = INF;
                ant[n] = i;
                for(int j = n; j != 1; j = ant[j])
                    mflow = min(mflow , F[ant[j]][j]);
                for(int j = n; j != 1; j = ant[j]){
                    F[ant[j]][j] -= mflow;
                    F[j][ant[j]] += mflow;

                }
                sol += mflow;
            }
        }

    }

    fout << sol;

    return 0;
}