Cod sursa(job #2698797)

Utilizator DordeDorde Matei Dorde Data 23 ianuarie 2021 01:54:09
Problema Critice Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.88 kb
#include <fstream>
#include <algorithm>
#include <queue>
using namespace std;
ifstream f ("critice.in");
ofstream g ("critice.out");
int const N = 1001 , M = 20001;
int c [N][N] , flow [N][N] , t [N] , e [N] , r [M] , X [M] , Y [M] , v [M] , nxt [M] , vf [M] , sz;
bool vizS [N] , vizD [N];
void add (int a , int b){
    vf [++ sz] = b;
    nxt [sz] = v [a];
    v [a] = sz;
}
bool bfs (int n){
    queue <int> q;
    q . push (1);
    e [1] = (1 << 30);
    fill (e + 2 , e + 1 + n , 0);
    fill (t + 1 , t + 1 + n , 0);
    while (q . size ()){
        int node = q . front ();
        for(int i = v [node] ; i ; i = nxt [i]){
            int y = vf [i];
            if (c [node][y] - flow [node][y] == 0 || e [y])
                continue;
            q . push (y);
            e [y] = min (c [node][y] - flow [node][y] , e [node]);
            t [y] = node;
        }
        q . pop ();
    }
    return t [n];
}
void DFS (int node){
    vizS [node] = true;
    for(int i = v [node] ; i ; i = nxt [i]){
        int y = vf [i];
        if (! vizS [y] && c [node][y] - flow [node][y] > 0)
            DFS (y);
    }
}
void DFD (int node){
    vizD [node] = true;
    for(int i = v [node] ; i ; i = nxt [i]){
        int y = vf [i];
        if (! vizD [y] && c [y][node] - flow [y][node] > 0)
            DFD (y);
    }
}
int main()
{
    int n , m;
    f >> n >> m;
    for(int i = 1 ; i <= m ; ++ i){
        int a , b , w;
        f >> a >> b >> w;
        add (a , b);
        c [a][b] = w;
        add (b , a);
        c [b][a] = w;
        X [i] = a;
        Y [i] = b;
    }
    while (bfs (n)){
        for(int i = 1 ; i < n ; ++ i){
            if (c [i][n] - flow [i][n] > 0){
                e [n] = min (e [i] , c [i][n] - flow [i][n]);
                if (! e [n])
                    continue;
                int x = i;
                while (t [x]){
                    if (c [t [x]][x] - flow [t [x]][x] < e [n])
                        e [n] = c [t [x]][x] - flow [t [x]][x];
                    x = t [x];
                }
                if (! e [n])
                    continue;
                x = i;
                flow [n][x] += e [n];
                flow [x][n] -= e [n];
                while (t [x]){
                    flow [t [x]][x] += e [n];
                    flow [x][t [x]] -= e [n];
                    x = t [x];
                }
            }
        }
    }
    DFS (1);
    DFD (n);
    for(int i = 1 ; i <= m ; ++ i){
        int from = X [i] , to = Y [i];
        int i1 = 2 * i - 1 , i2 = 2 * i;
        if (((c [to][from] - flow [to][from] == 0) || (c [from][to] - flow [from][to] == 0)) && ((vizS [from] && vizD [to]) || (vizD [from] && vizS [to])))
            r [++ r [0]] = i;
    }
    g << r [0] << '\n';
    for(int i = 1 ; i <= r [0] ; ++ i)
        g << r [i] << '\n';
    return 0;
}