Cod sursa(job #2212499)

Utilizator felixiPuscasu Felix felixi Data 14 iunie 2018 12:12:39
Problema Critice Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.65 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("critice.in");
ofstream out("critice.out");

const int NMAX = 1000;
const int INF  = (1 << 30);

vector<int> G[NMAX+2];
vector<pair<int,int>> edge;
int cap[NMAX+2][NMAX+2], flux[NMAX+2][NMAX+2];
int tata[NMAX+2];
int N, M;
///int inCap[NMAX+2], outCap[NMAX+2];
bool from[NMAX+2], to[NMAX+2];


bool bfs(int start, int finish)
{
    bool viz[NMAX+2];
    memset(viz, 0, sizeof(viz));
    queue<int> Q;
    Q.push(start);
    viz[start] = 1;
    while( !Q.empty() ) {
        int nod = Q.front();
        Q.pop();
        for( auto x : G[nod] ) {
            if( viz[x] || flux[nod][x] == cap[nod][x] ) continue;
            viz[x] = 1;
            tata[x] = nod;
            Q.push(x);
        }
    }
    return viz[finish];
}

void get_flux(int start, int finish)
{
    while( bfs(start, finish) ) {
        for( auto x : G[finish] ) {
            tata[finish] = x;

            int flow = INF;
            for( int nod = finish;  nod != start;  nod = tata[nod] )
                flow = min(flow, cap[ tata[nod] ][nod] - flux[ tata[nod] ][nod]);
            for( int nod = finish;  nod != start;  nod = tata[nod] ) {
                flux[ tata[nod] ][nod] += flow;
                flux[nod][ tata[nod] ] += flow;
            }
        }
    }
}

void fromStart(int st)
{
    queue<int> Q;
    Q.push(st);
    from[st] = 1;
    while( !Q.empty() ) {
        int nod = Q.front();
        Q.pop();
        for( auto x : G[nod] ) {
            if( flux[nod][x] < cap[nod][x] && !from[x] ) {
                from[x] = 1;
                Q.push(x);
            }
        }
    }
}

void fromEnd(int st)
{
    queue<int> Q;
    Q.push(st);
    to[st] = 1;
    while( !Q.empty() ) {
        int nod = Q.front();
        Q.pop();
        for( auto x : G[nod] ) {
            if( flux[nod][x] < cap[nod][x] && !to[x] ) {
                to[x] = 1;
                Q.push(x);
            }
        }
    }
}

int main()
{
    in >> N >> M;
    for( int i = 1;  i <= M;  ++i ) {
        int x,y,c;
        in >> x >> y >> c;
        edge.push_back({x,y});
        ///in >> x >> y >> c;
        cap[x][y] = cap[y][x] = c;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    get_flux(1, N);
    fromStart(1);
    fromEnd(N);
    vector<int> sol;
    for( int i = 0;  i < M;  ++i ) {
        int a = edge[i].first, b = edge[i].second;
        if( (from[a] && to[b]) || (from[b] && to[a]) )
            sol.push_back(i + 1);
       /// if(  )
    }
    out << sol.size() << '\n';
    for( auto x : sol ) out << x << '\n';
    return 0;
}