Cod sursa(job #2697670)

Utilizator theo2003Theodor Negrescu theo2003 Data 19 ianuarie 2021 11:56:10
Problema Flux maxim Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.58 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(unsigned int x = 0;x<g[bi[bx]].size();x++){
            auto &p = g[bi[bx]][x];
            if(p.second < 0)
                throw(1);
            if((p.second > 0) && (p.first == (n))){
                int minf = p.second;
                for(short int x = bi[bx];x != 1;x = bp[x]){
                    if((*bf[x]) < minf)
                        minf = *bf[x];
                }
                p.second -= minf;
                for(short int x = bi[bx];x != 1;x = bp[x]){
                    (*bf[x]) -= minf;
                    if((*bf[x]) < 0)
                        throw(1);
                }
                return minf;
            }
            if((p.second > 0) && (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;
}