Cod sursa(job #2697668)

Utilizator theo2003Theodor Negrescu theo2003 Data 19 ianuarie 2021 11:45:26
Problema Flux maxim Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
vector<pair<short int, int> > g[1010];
int bi[1010], bv[1010], bp[1010], bs, bvnr = 1, bx = 0;
int* bf[1010];
short int n, m;
int bfs(){
    bs = 1;
    bx = 0;
    bi[0] = 1;
    bvnr++;
    bv[1] = bvnr;
    while(bx < bs){
        for(int x = 0;x<g[bi[bx]].size();x++){
            auto &p = g[bi[bx]][x];
            if((p.second) && (p.first == (n))){
                int minf = *bf[bi[bx]];
                for(short int x = bi[bx];x != 1;x = bp[x]){
                    if((*bf[x]) < minf)
                        minf = *bf[x];
                }
                for(short int x = bi[bx];x != 1;x = bp[x]){
                    (*bf[x]) -= minf;
                }
                return minf;
            }
            if((p.second) && (bv[p.first] != bvnr)){
                bv[p.first] = bvnr;
                bi[bs++] = p.first;
                bp[p.first] = bi[bx];
                bf[p.first] = &p.second;
            }
        }
        bx++;
    }
    return 0;
}
int main(){
    in>>n>>m;
    long long outv = 0;
    for(short int x = 0;x<m;x++){
        short int a, b, c;
        in>>a>>b>>c;
        g[a].push_back({b, c});
    }
    int tmp;
    do{
        tmp = bfs();
        outv += tmp;
    }while(tmp);
    out<<outv<<endl;
    return 0;
}