Cod sursa(job #2659234)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 16 octombrie 2020 10:20:26
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
//ca sursa de dinainte doar ca cu bfs facut cu queue

#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];
vector<int> L[1100];
bitset<1100> viz;
queue<int> q;
int bfs(){ //vad daca mai pot ajunge de la sursa la destinatie pe muchii nesaturate
    viz.reset();
    viz[1] = 1;
    q.push(1);
    int dest = 0;
    while ( !q.empty() ) {
        nod = q.front();
        for( auto vecin: L[nod] )
            if( !viz[vecin] && c[nod][vecin] > flux[nod][vecin] ){
                viz[vecin] = 1;
                q.push(vecin);
                t[vecin] = nod;
                if ( vecin == n )
                    dest=1;
            }
        q.pop();
    }
    return dest;
}

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;
}