Cod sursa(job #2698154)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 21 ianuarie 2021 10:09:37
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.95 kb
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 1005;
vector <int> vec[NMAX];
struct ABC {
    int x, y;
};
vector <ABC> muchii;
int cost[NMAX][NMAX], viz[NMAX];
queue <int> q;
int n, m;
int eFlux() {
    for(int i = 1; i <= n; i++) {
        viz[i] = 0;
    }
    while(!q.empty()) {
        q.pop();
    }
    q.push(1);
    while(!q.empty()) {
        int a = q.front();
        q.pop();
        for(int i = 0; i < vec[a].size(); i++) {
            if(viz[vec[a][i]] == 0 && cost[a][vec[a][i]]) {
                q.push(vec[a][i]);
                viz[vec[a][i]] = a;
            }
        }
    }
    return viz[n];
}
void init() {
    while(!q.empty()) {
        q.pop();
    }
}
int viz1[NMAX], viz2[NMAX];
void verif1(int x) {
    init();
    q.push(x);
    viz1[x] = 1;
    while(!q.empty()) {
        int a = q.front();
        q.pop();
        for(int i = 0; i < vec[a].size(); i++) {
            if(viz1[vec[a][i]] == 0 && cost[a][vec[a][i]] && cost[vec[a][i]][a]) {
                viz1[vec[a][i]] = 1;
                q.push(vec[a][i]);
            }
        }
    }
}
void verif2(int x) {
    init();
    q.push(x);
    viz2[x] = 1;
    while(!q.empty()) {
        int a = q.front();
        q.pop();
        for(int i = 0; i < vec[a].size(); i++) {
            if(viz2[vec[a][i]] == 0 && cost[a][vec[a][i]] && cost[vec[a][i]][a]) {
                viz2[vec[a][i]] = 1;
                q.push(vec[a][i]);
            }
        }
    }
}
vector <int> sol;

int main() {
    freopen("critice.in", "r", stdin);
    freopen("critice.out", "w", stdout);
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= m; i++) {
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        vec[x].push_back(y);
        vec[y].push_back(x);
        muchii.push_back({x, y});
        cost[x][y] = c;
        cost[y][x] = c;
    }
    while(eFlux()) {
        for(int i = 1; i < n; i++) {
            if(viz[i] && cost[i][n]) {
                int minval = cost[i][n], j = i;
                while(j != 1) {
                    minval = min(minval, cost[viz[j]][j]);
                    j = viz[j];
                }
                if(minval) {
                    cost[i][n] -= minval;
                    cost[n][i] += minval;
                    j = i;
                    while(j != 1) {
                        cost[viz[j]][j] -= minval;
                        cost[j][viz[j]] += minval;
                        j = viz[j];
                    }
                }
            }
        }
    }
    verif1(1);
    verif2(n);
    for(int i = 0; i < m; i++) {
        ABC a = muchii[i];
        if((viz1[a.x] && viz2[a.y]) || (viz1[a.y] && viz2[a.x])) {
            sol.push_back(i + 1);
        }
    }
    printf("%d\n", sol.size());
    for(int i = 0; i < sol.size(); i++) {
        printf("%d\n", sol[i]);
    }
    return 0;
}