Cod sursa(job #2960263)

Utilizator soda-popClinciu Diana soda-pop Data 3 ianuarie 2023 21:55:12
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 6.66 kb
#include <climits>
#include <vector>
#include <fstream>
#include <queue>
using namespace std;

ifstream in("maxflow.in");
ofstream out("maxflow.out");

int BFS(int s, int n, vector<vector<int>> &adjList, vector<vector<int>> &adjListIn, 
        vector<vector<long>> &flux, vector<vector<long>> &cost, vector<int> &tata) {
    queue<int> queue;
    vector<int> viz(n+1, 0);

    for (int i = 0; i <= n; i++)
        tata[i] = 0;

    queue.push(s);
    viz[s] = 1;

    while (!queue.empty()) {
        int x;

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

        for (auto it = adjList[x].begin(); it != adjList[x].end(); ++it) {
            int y;
            y = *it;
            if (viz[y] == 0 && flux[x][y] < cost[x][y]) {
                queue.push(y);
                viz[y] = 1;
                tata[y] = x;
                if (y == n) return 1;
            }
        }

        for (auto it = adjListIn[x].begin(); it != adjListIn[x].end(); ++it) {
            int y;
            y = *it;
            if (viz[y] == 0 && flux[y][x] > 0) {
                queue.push(y);
                viz[y] = 1;
                tata[y] = -x;
                if (y == n) return 1;
            }
        }
    }
    return 0;
}

int main() {
    int n, m;

    // citire date intrare
    in >> n >> m;

    vector<vector<int>> adjList(n+1);
    vector<vector<int>> adjListIn(n+1);
    vector<vector<long>> flux(n+1, vector<long> (n+1, 0)); //!!!
    vector<vector<long>> cost(n+1, vector<long> (n+1, 0));

    for (int i = 1; i <= m; i++) {
        int x, y, c;
        in >> x >> y >> c;

        adjList[x].push_back(y);
        adjListIn[y].push_back(x);  // liste adiacenta intrare
        cost[x][y] = c;
    }

    long fluxMax = 0;
    int s = 1;  // sursa
    int t = n;  // destinatia
    vector<int> tata(n+1);

    while (BFS(s, n, adjList, adjListIn, flux, cost, tata)) {
        // calculam i(P) = capacitatea reziduala minima pe un arc de pe drumul
        // de adjList s adjList t determinat cu bf
        long iP = LONG_MAX;  // i(P)
        t = n;
        while (t != s) {
            if (tata[t] >= 0) {  // arc direct - capacitate c(e)-flux(e)
                if (cost[tata[t]][t] - flux[tata[t]][t] < iP)
                    iP = cost[tata[t]][t] - flux[tata[t]][t];
                t = tata[t];
            } else {  // arc invers - capacitate flux(e)
                if (flux[t][-tata[t]] < iP) iP = flux[t][-tata[t]];
                t #include <climits>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

ifstream in("cuplaj.in");
ofstream out("cuplaj.out");

int BFS(int s, int n, vector<vector<int>> &adjList,
        vector<vector<int>> &adjListIn, vector<vector<bool>> &flux,
        vector<vector<bool>> &cost, vector<int> &tata) {
    queue<int> queue;
    vector<bool> viz(n + 1, 0);

    for (int i = 0; i <= n; i++) tata[i] = 0;

    queue.push(s);
    viz[s] = 1;

    while (!queue.empty()) {
        int x;

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

        for (auto it = adjList[x].begin(); it != adjList[x].end(); ++it) {
            int y;
            y = *it;
            if (viz[y] == 0 && flux[x][y] < cost[x][y]) {
                queue.push(y);
                viz[y] = 1;
                tata[y] = x;
                if (y == n) return 1;
            }
        }

        for (auto it = adjListIn[x].begin(); it != adjListIn[x].end(); ++it) {
            int y;
            y = *it;
            if (viz[y] == 0 && flux[y][x] > 0) {
                queue.push(y);
                viz[y] = 1;
                tata[y] = -x;
                if (y == n) return 1;
            }
        }
    }
    return 0;
}

int main() {
    int N, M, E;

    // citire date intrare
    in >> M >> N >> E;

    int n = N + M;
    vector<vector<int>> adjList(n + 3);
    vector<vector<int>> adjListIn(n + 3);
    vector<vector<bool>> flux(n + 3, vector<bool>(n + 3, 0));  //!!!
    vector<vector<bool>> cost(n + 3, vector<bool>(n + 3, 0));

    for (int i = 1; i <= E; i++) {
        int x, y;
        in >> x >> y;
        adjList[x].push_back(y + M);
        adjListIn[y + M].push_back(x);
        cost[x][y + M] = 1;
    }

    int s = n + 1;
    int t = n + 2;
    n = n + 2;
    for (int i = 1; i <= M; i++) {
        adjList[s].push_back(i);
        adjListIn[i].push_back(s);
        cost[s][i] = 1;
    }
    for (int i = M + 1; i <= N + M; i++) {
        adjList[i].push_back(t);
        adjListIn[t].push_back(i);
        cost[i][t] = 1;
    }

    int fluxMax = 0;
    vector<int> tata(n + 1);

    while (BFS(s, n, adjList, adjListIn, flux, cost, tata)) {
        // calculam i(P) = capacitatea reziduala minima pe un arc de pe drumul
        // de adjList s adjList t determinat cu bf
        bool iP = 1;  // i(P)
        t = n;
        while (t != s) {
            if (tata[t] >= 0) {  // arc direct - capacitate c(e)-flux(e)
                if (cost[tata[t]][t] - flux[tata[t]][t] < iP)
                    iP = cost[tata[t]][t] - flux[tata[t]][t];
                t = tata[t];
            } else {  // arc invers - capacitate flux(e)
                if (flux[t][-tata[t]] < iP) iP = flux[t][-tata[t]];
                t = -tata[t];
            }
        }

        // revizuim fluxul de-a lungul lantului determinat
        t = n;
        while (t != s) {
            if (tata[t] >= 0) {  // arc direct - creste fluxul cu iP
                flux[tata[t]][t] = flux[tata[t]][t] + iP;
                t = tata[t];
            } else {  // arc invers - scade fluxul cu iP
                flux[t][-tata[t]] = flux[t][-tata[t]] - iP;
                t = -tata[t];
            }
        }

        fluxMax += iP;  // creste valoarea fluxului cu iP
    }

    out << fluxMax << '\n';

    for (int x = 1; x <= n - 2; x++) {
        for (int j = 0; j < adjList[x].size(); j++) {
            int y = adjList[x][j];
            if (flux[x][y] && y != n) out << x << ' ' << y - M << '\n';
        }
    }

    out.close();
    in.close();
    return 0;
}= -tata[t];
            }
        }

        // revizuim fluxul de-a lungul lantului determinat
        t = n;
        while (t != s) {
            if (tata[t] >= 0) {  // arc direct - creste fluxul cu iP
                flux[tata[t]][t] += iP;
                t = tata[t];
            } else {  // arc invers - scade fluxul cu iP
                flux[t][-tata[t]] -= iP;
                t = -tata[t];
            }
        }

        fluxMax += iP;  // creste valoarea fluxului cu iP
    }

    out << fluxMax;

    out.close();
    in.close();
    return 0;
}