Cod sursa(job #2764919)

Utilizator DragosC1Dragos DragosC1 Data 23 iulie 2021 16:04:45
Problema Critice Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.62 kb
#include <fstream>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;

int C[1001][1001], F[1001][1001];
int n, m;
pair<int, int> mch[10001];
int nr;
int t[1001];
const int Inf = 1e9;

vector<int> a[1001];

void read() {
    int x, y, c, i;
    ifstream f("critice.in");
    f >> n >> m;
    for (i = 1; i <= m; i++) {
        f >> x >> y >> c;
        a[x].emplace_back(y);
        a[y].emplace_back(x);
        C[x][y] = C[y][x] = c;
        mch[i] = {x, y};
    }
    f.close();
}

queue<int> Q;
bool viz[1001];

bool bfs() {
    int i, x, fiu;
    for (i = 1; i <= n; i++)
        viz[i] = 0;
    Q.push(1);
    viz[1] = 1;
    while (!Q.empty()) {
        x = Q.front();
        Q.pop();
        if (x == n) continue;
        for (i = 0; i < a[x].size(); i++) {
            fiu = a[x][i];
            if (!viz[fiu] && C[x][fiu] != F[x][fiu]) {
                viz[fiu] = 1;
                t[fiu] = x;
                Q.push(fiu);
            }
        }
    }
    return viz[n];
}

vector<int> critice;

bool last[1001];
bool first[1001];

void bfs1(int x, bool ap[]) {
    int i, fiu;
    Q.push(x);
    ap[x] = 1;
    while (!Q.empty()) {
        x = Q.front();
        Q.pop();
        for (i = 0; i < a[x].size(); i++) {
            fiu = a[x][i];
            if (!ap[fiu] && C[x][fiu] > F[x][fiu]) {
                ap[fiu] = 1;
                Q.push(fiu);
            }
        }
    }
}

void solve() {
    int i, flow, nod, j, x, y;
    while (bfs())
        for (i = 0; i < a[n].size(); i++) {
            if (!viz[a[n][i]] || C[a[n][i]][n] == F[a[n][i]][n])
                continue;
            t[n] = a[n][i];
            nod = n;
            flow = Inf;
            while (nod != 1) {
                flow = min(flow, C[t[nod]][nod] - F[t[nod]][nod]);
                nod = t[nod];
            }
            nod = n;
            while (nod != 1) {
                F[t[nod]][nod] += flow;
                F[nod][t[nod]] -= flow;
                nod = t[nod];
            }
        }
    bfs1(1, first);
    bfs1(n, last);
    for (i = 1; i <= m; i++) {
        x = mch[i].first, y = mch[i].second;
        if ((first[x] && last[y]) || (first[y] && last[x])) 
            if (C[x][y] == F[x][y] || C[y][x] == F[y][x])
                critice.push_back(i);
    }
}

void output() {
    int i;
    ofstream g("critice.out");
    g << critice.size() << '\n';
    for (i = 0; i < critice.size(); i++)
        g << critice[i] << '\n';
    g.close();
}

int main() {
    read();
    solve();
    output();
    return 0;
}