Cod sursa(job #2697197)

Utilizator dariahazaparuHazaparu Daria dariahazaparu Data 17 ianuarie 2021 19:40:12
Problema Critice Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.52 kb
#include<fstream>
#include<vector>
#include<queue>
#include<set>

const long long N_MAX = 10005;
const long long INF = 1LL << 60;

int n, m, S, F;
std::vector<int> graph[N_MAX];
int capacitate[N_MAX][N_MAX];
int flow[N_MAX][N_MAX];
int muchii[N_MAX][N_MAX];
int tata[N_MAX];

int bfs() {
    for (int i = 1; i <= n; i++)
        tata[i] = 0;
    std::queue<int> q;
    q.push(S);
    tata[S] = S;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        for (auto next : graph[cur]) {
            if (!tata[next] && (capacitate[cur][next] - flow[cur][next] > 0)) {
                tata[next] = cur;
                if (next != F)
                    q.push(next);
            }
        }
    }
    if (tata[F])
        return 1;

    return 0;
}

int FKarp() {
    int maxflow = 0;
    while (bfs()) {
        for (auto vecin : graph[F])
            if (tata[vecin] && capacitate[vecin][F] - flow[vecin][F] > 0) {
                int currentFlow = 2e9;
                tata[F] = vecin;
                int current = F;
                int previous;
                while (current != S) {
                    previous = tata[current];
                    currentFlow = std::min(currentFlow, capacitate[previous][current] - flow[previous][current]);
                    current = previous;
                }
                current = F;
                if (currentFlow > 0)
                    while (current != S) {
                        previous = tata[current];
                        flow[previous][current] += currentFlow;
                        flow[current][previous] -= currentFlow;
                        current = previous;
                    }
                maxflow += currentFlow;
            }
    }
    return maxflow;
}

int main() {
    std::ifstream fin("critice.in");
    std::ofstream fout("critice.out");
    fin >> n >> m;
    S = 1;
    F = n;
    for (int i = 1; i <= n; i++) {
        int x, y, z;
        fin >> x >> y >> z;
        graph[x].push_back(y);
        graph[y].push_back(x);
        capacitate[x][y] = capacitate[y][x] = z;
        muchii[x][y] = muchii[y][x] = i;
    }
    FKarp();
    std :: set<int> rez;
    for (int i = 1; i <= n; i++)
        if (tata[i])
            for (auto j : graph[i])
                if (!tata[j] and capacitate[i][j] - flow[i][j] == 0)
                    rez.insert(muchii[i][j]);

    fout << rez.size() << '\n';
    for (auto temp: rez)
        fout << temp << '\n';

    return 0;
}