Cod sursa(job #2663546)

Utilizator mihnea.anghelMihnea Anghel mihnea.anghel Data 26 octombrie 2020 18:27:19
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
#define f in
#define g out

using namespace std;
ifstream in ( "critice.in" );
ofstream out( "critice.out" );
int n, m, i, j, x, y, z, dif, nod, k;
int t[1100], c[1100][1100], flux[1100][1100], v[2][1100], sol[10100];
vector<int> L[1100];
pair<int, int> mch[10100];
bitset<1100> viz;
queue<int> q;

int bfs(){
    viz.reset();
    q.push(1);
    viz[1] = 1;
    while ( !q.empty() ) {
        nod = q.front();
        q.pop();
        for ( auto vecin: L[nod] )
            if ( !viz[vecin] && c[nod][vecin] > flux[nod][vecin] ){
                viz[vecin] = 1;
                q.push(vecin);
                t[vecin] = nod;
            }
    }
    return viz[n];
}

void bfs2 ( int nod, int x ){
    viz.reset();
    viz[nod] = 1;
    q.push(nod);
    v[x][nod] = 1;
    while ( !q.empty() ) {
        nod = q.front();
        q.pop();
        for ( auto vecin: L[nod] )
            if ( !viz[vecin] && c[nod][vecin] > max (flux[nod][vecin], -flux[nod][vecin]) ){
                viz[vecin] = 1;
                q.push(vecin);
                v[x][vecin] = 1;
            }
    }
}

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] = c[y][x] = z;
        mch[i] = {x,y};
    }
    
    while(bfs()){
        for ( auto vecin: L[n] ){
            dif = c[vecin][n] - flux[vecin][n];
            if ( !viz[vecin] || dif <= 0 )
                continue;
            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;
            
            x = vecin;
            while ( t[x] ) {
                flux[t[x]][x] += dif;
                flux[x][t[x]] -= dif;
                x = t[x];
            }
        }
    }
    
    bfs2(1, 0);
    bfs2(n, 1);
    
    for ( i=1; i <= m; i++ ){
        x = mch[i].first;
        y = mch[i].second;
        if ( (v[0][x] && v[1][y]) || (v[1][x] && v[0][y] ) )
            sol[++k] = i;
    }
    g<<k<<"\n";
    for ( i=1; i <= k; i++ )
        g<<sol[i]<<"\n";
    return 0;
}