Cod sursa(job #2659230)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 16 octombrie 2020 10:13:51
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <bitset>
#include <vector>
#include <queue>
#define f in
#define g out

using namespace std;
ifstream in ( "maxflow.in" );
ofstream out( "maxflow.out" );
int n, m, i, x, y, z, dif, fluxmax, p, u, nod;
int t[1100], flux[1100][1100], c[1100][1100], q[1100];
vector<int> L[1100];
bitset<1100> viz;

int bfs(){ //vad daca mai pot ajunge de la sursa la destinatie pe muchii nesaturate
    viz.reset();
    p = u = viz[1] = q[1] = 1;
    while ( p <= u ) {
        nod = q[p];
        for( auto vecin: L[nod] )
            if( !viz[vecin] && c[nod][vecin] > flux[nod][vecin] ){
                viz[vecin] = 1;
                q[++u] = vecin;
                t[vecin] = nod;
                if ( vecin == n )
                    return 1;
            }
        p++;
    }
    return 0;
}

int main() {
    f>>n>>m;
    for ( i=1; i <= m; i++ ){
        f>>x>>y>>z;
        L[x].push_back(y);
        L[y].push_back(x);
        c[x][y] = z; //capacitatea
        // cost[y][x] = 0 muchia inversa fictiva
    }
    while ( bfs() ){
        
        dif = 2000000000;
        x = n;
        while ( t[x] ) {
            dif = min ( dif, c[t[x]][x] - flux[t[x]][x] );
            x = t[x];
        }
        
        fluxmax += dif;
        
        // cresc fluxul
        x = n;
        while ( t[x] ) {
            flux[t[x]][x] += dif;
            flux[x][t[x]] -= dif;
            x = t[x];
        }
        
    }
    g<<fluxmax;
    return 0;
}