Cod sursa(job #2662219)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 23 octombrie 2020 18:08:23
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.81 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() ){
        
        for ( auto vecin: L[n] ){
            if ( viz[vecin] && c[vecin][n] > flux[vecin][n] ){
                dif = c[vecin][n]-flux[vecin][n];
                x = vecin;
                while ( t[x] ) {
                    dif = min( dif, c[t[x]][x]-flux[t[x]][x] );
                    x = t[x];
                }
                flux[vecin][n] += dif;
                flux[n][vecin] -= dif;
                fluxmax += dif;
                
                x = vecin;
                while ( t[x] ) {
                    flux[t[x]][x] += dif;
                    flux[x][t[x]] -= dif;
                    x = t[x];
                }
            }
        }
    }
    g<<fluxmax;
    return 0;
}