Cod sursa(job #2986865)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 1 martie 2023 13:38:00
Problema Critice Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

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

const int MAX = 1e3 + 1;

const int inf = 1e9 + 1;

int c[MAX][MAX] , f[MAX][MAX] , n , m , x , y , cap , level[MAX] , ef[MAX] , ind[MAX][MAX];

bool viz[MAX];

vector <int> g[MAX];

bool bfs(){

    queue <int> q;

    for(int i = 1 ; i <= n ; i++){

        level[i] = ef[i] = 0;
    }

    q.push(1);

    level[1] = 1;

    while(!q.empty()){

        int x = q.front();

        q.pop();

        for(auto it : g[x]){

            if(!level[it] && (c[x][it]-f[x][it])>0){

                level[it] = level[x]+1;

                if(it == n) return 1;

                q.push(it);
            }
        }
    }

    return 0;

}

int dfs( int x , int flow ){

    if(x == n){

        return flow;
    }

    int sz = g[x].size();

    for(; ef[x] < sz ; ef[x]++){

        int it = g[x][ef[x]];

        if(level[it] == level[x] + 1 && (c[x][it]-f[x][it] > 0)){

            int new_flow = dfs(it,min(flow,c[x][it]-f[x][it]));

            if(new_flow){

                f[x][it] += new_flow;
                f[it][x] -= new_flow;

                return new_flow;
            }
        }
    }

    return 0;

}

vector <int> muchii;
vector <pair<int,int>> aux;

void bfs2(){

    queue <int> q;

    q.push(1);

    while(!q.empty()){

        int x = q.front();

        q.pop();

        viz[x] = 1;

        for(auto it : g[x]){

            if(!viz[it]){

                if(c[x][it] - f[x][it] > 0){

                    q.push(it);

                }else aux.push_back({x,it});
            }
        }
    }
}

int main(){

    cin >> n >> m;

    for(int i = 1 ; i <= m ; i++){

        cin >> x >> y >> cap;
        g[x].push_back(y);
        g[y].push_back(x);
        c[x][y] = cap;
        ind[x][y] = i;
        ind[y][x] = i;
    }

    while(bfs()){

        while(1){

            int x = dfs(1,inf);

            if(!x) break;
        }
    }

    for(int i = 1 ; i <= n ; i++){

        viz[i] = 0;
    }

    bfs2();

    for(auto it : aux){

        if(!viz[it.second]){

            muchii.push_back(ind[it.first][it.second]);
        }
    }

    sort(muchii.begin(),muchii.end());


    cout << muchii.size() << '\n';

    for(auto it : muchii){

        cout << it << '\n';
    }

    return 0;
}