Cod sursa(job #3187420)

Utilizator DRGmenMarcu Dragos-Ionut DRGmen Data 28 decembrie 2023 20:51:22
Problema Cc Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.88 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("cc.in");
ofstream fout("cc.out");

int costs[202][202];

class Graf {
    vector<vector<int>> muchii;
    map<pair<int, int>, int> val_muchii;
    int nr_noduri;
public:

    Graf(int n = 1) : nr_noduri(n) {
        muchii = vector<vector<int>>(n + 1);
    }

    void add_muchie(int x, int y, int _cost = 0) {
        muchii[x].push_back(y);
        val_muchii[{x, y}] = _cost;
    }

    void set_val_muchie(int x, int y, int newval) {
        val_muchii[{x, y}] = newval;
    }

    int get_val_muchie(int x, int y) {
        return val_muchii[{x, y}];
    }

    void flux_bfs(vector<int> &parinti, int start, int sink) {
        queue<pair<int, int>> q;
        q.push({start, INT_MAX});
        vector<int> costmin(nr_noduri, INT_MAX), inqueue(nr_noduri, 0);
        parinti[start] = start;
        costmin[start] = 0;
        while (!q.empty()) {
            int nodc = q.front().first, fluxmax = q.front().second;
            //cout << nodc << " | ";
            for (auto &v: muchii[nodc]) {
                //cout << val_muchii[{nodc, v}] << "," << v << "," << costmin[v] << ' ';
                if (val_muchii[{nodc, v}] > 0 && costmin[v] > costmin[nodc] + costs[nodc][v]) {
                    costmin[v] = costmin[nodc] + costs[nodc][v];
                    parinti[v] = nodc;
                    if (!inqueue[v]) {
                        inqueue[v] = 1;
                        q.push({v, min(val_muchii[{nodc, v}], fluxmax)});
                    }
                }
            }
            //cout << '\n';
            q.pop();
        }
    }

    void flux(int n) {
        int r = 0;
        vector<int> parinti(202, -1);
        parinti[201] = 201;
        while (parinti[201] != -1) {
            parinti.assign(202, -1);
            flux_bfs(parinti, 0, 201);
            if (parinti[201] != -1) {
                int nodc = 201;
                while (nodc != 0) {
                    val_muchii[{parinti[nodc], nodc}] -= 1;
                    val_muchii[{nodc, parinti[nodc]}] += 1;
                    nodc = parinti[nodc];
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (val_muchii[{100 + j, i}] > 0) {
                    cout << i << ' ' << j << '\n';
                    r += costs[i][100 + j];
                }
            }
        }
        fout << r;
    }

};

int main() {
    int n;
    fin >> n;
    Graf g(202);
    for (int i = 1; i <= n; i++) {
        g.add_muchie(0, i, 1);
        g.add_muchie(i, 0, 0);
        g.add_muchie(100 + i, 201, 1);
        g.add_muchie(201, 100 + i, 0);
        for (int j = 1; j <= n; j++) {
            fin >> costs[i][100 + j];
            costs[100 + j][i] -= costs[i][100 + j];
            g.add_muchie(i, 100 + j, 1);
            g.add_muchie(100 + j, i, 0);
        }
    }
    g.flux(n);
    return 0;
}