Cod sursa(job #3038288)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 27 martie 2023 10:33:56
Problema Critice Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

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

const int MAX = 1e3 + 1;

const int inf = 1e9 + 1;

int f[MAX][MAX] , cap[MAX][MAX] , ind[MAX][MAX] , n , m , x , y , r;

bool ajunge[MAX];

int level[MAX] , ef[MAX];

vector <int> g[MAX];

struct Dinic{

    bool bfs( int source , int sink ){

        queue <int> q;

        bool ok = 0;

        q.push(source);

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

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

        level[source] = 0;

        int x;

        while(!q.empty()){

            x = q.front();
            q.pop();

            for(auto it : g[x]){

                if(level[it] == inf && (cap[x][it] - f[x][it])){

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

                    if(it == sink) ok = 1;

                    q.push(it);
                }
            }
        }

        return ok;
    }

    int dfs( int x , int sink , int flow){

        if( x == sink ){

            return flow;
        }

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

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

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

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

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

                new_flow = dfs(it,sink,new_flow);

                if(new_flow){

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

                    return new_flow;
                }
            }
        }

        return 0;
    }
}d;

vector <int> muchii;

int main(){

    cin >> n >> m;

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

        cin >> x >> y >> r;

        g[x].push_back(y);
        g[y].push_back(x);

        cap[x][y] = r;

        ind[x][y] = i;
    }

    while(d.bfs(1,n)){

        int new_flow;

        while(1){

            new_flow = d.dfs(1,n,1e9 + 1);

            if(!new_flow) break;
        }
    }

    d.bfs(1,n);

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

        if(level[i] == inf) continue;

        for(auto it : g[i]){

            if(level[it] == inf && cap[i][it]){

                muchii.push_back(ind[i][it]);
            }
        }
    }

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

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

    for(auto it : muchii){

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

    return 0;
}