Cod sursa(job #1878299)

Utilizator DobosDobos Paul Dobos Data 13 februarie 2017 23:58:50
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 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){
                if(i == n)
                    return 1;
                Q.push_back(i);
                B[i] = 1;
                ant[i] = nod;
            }
        }
    }

    return 0;
}


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]){
            mflow = INF;
            for(int j = i; j != ant[j]; j = ant[j])
                mflow = min(mflow , F[ant[j]][j]);
            for(int j = i; j != ant[j]; j = ant[j]){
                F[ant[j]][j] -= mflow;
                F[j][ant[j]] += mflow;

            }
            sol += mflow;
        }

    }

    fout << sol;

    return 0;
}