Cod sursa(job #2811751)

Utilizator linte_robertLinte Robert linte_robert Data 3 decembrie 2021 00:27:38
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <utility>
#include <queue>
using namespace std;

int flux[1010][1010];
int c[1010][1010];
int tata[1010];
int viz[1010], flow;
int coad[1015];
int Q, x, y, z, N, act, M, fmin;

vector < int > g[1010];

int BF(){
    for( int i = 1; i <= N; i++ ){
        viz[i] = 0;
    }

    coad[0] = 1;
    int st = 0, dr = 1;
    viz[1] = 1;

    while( st <dr ){
        act = coad[st];
        if( act != N ){
            for( int i = 0; i< g[act].size(); i++ ){
                Q = g[act][i];
                if( c[act][Q] == flux[act][Q] || viz[Q] ){
                    continue;
                }
                viz[Q] = 1;
                coad[dr++] = Q;
                tata[Q] = act;
            }

        }
        st++;
    }
    return viz[N];
}

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

    fin >> N >> M;

    for( int i = 1; i <= M; i++ ){
        fin >> x >> y >> z;
        c[x][y] += z;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for( flow = 0; BF(); ){
        for( int i = 0;i < g[N].size(); i++ ){
            act = g[N][i];
            if( flux[act][N] == c[act][N] || !viz[act] ){
                continue;
            }
            tata[N] = act;
            fmin = 10101000;
            for( int nod = N; nod != 1; nod = tata[nod] ){
                fmin = min( fmin, c[tata[nod] ][nod] - flux[tata[nod] ][nod] );
            }
            if( fmin == 0 ){
                continue;
            }
            for( int nod = N; nod != 1; nod = tata[nod] ){
                flux[ tata[nod] ][nod] += fmin;
                flux[ nod ][ tata[nod] ] -= fmin;
            }
            flow += fmin;
        }
    }
    fout << flow;
}