Cod sursa(job #3190029)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 6 ianuarie 2024 20:20:47
Problema Cc Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.17 kb
//ALEXANDRU MICLEA
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

//VARIABLES

int inf = 1e9 + 7;
const int nmax = 205;
int n, m;

int capacitate[nmax][nmax];
int flux[nmax][nmax];
int cost[nmax][nmax];

int drum[nmax];
int tata[nmax];
int nod_terminal;

vector<int> gr[nmax];

//FUNCTIONS

int drum_ameliorare() {
    for (int i = 0; i <= nod_terminal; i++) {
        tata[i] = -1;
        drum[i] = inf;
    }

    tata[0] = -2;
    drum[0] = 0;

    queue<pair<int,int>> q;
    q.push({0, inf});

    while (!q.empty()) {
        int nod_curent = q.front().first;
        int flux_curent = q.front().second;
        q.pop();
        for (auto& nod_adiacent : gr[nod_curent]) {
            if (capacitate[nod_curent][nod_adiacent] - flux[nod_curent][nod_adiacent] > 0 && drum[nod_adiacent] > drum[nod_curent] + cost[nod_curent][nod_adiacent]){
                tata[nod_adiacent] = nod_curent;
                int flux_nou = min(flux_curent, capacitate[nod_curent][nod_adiacent] - flux[nod_curent][nod_adiacent]);
                drum[nod_adiacent] = drum[nod_curent] + cost[nod_curent][nod_adiacent];
                if (nod_adiacent == nod_terminal) return flux_nou;
                q.push({nod_adiacent, flux_nou});
            }
        }
    }
    return 0;
}

//MAIN
int main() {

//#define FILE_INPUT
#ifdef INFOARENA
    ifstream fin("cc.in"); ofstream fout("cc.out");
    cin.rdbuf(fin.rdbuf()); cout.rdbuf(fout.rdbuf());
#endif
#ifdef FILE_INPUT
    ifstream fin("file.in"); cin.rdbuf(fin.rdbuf());
    ofstream fout("file.out"); cout.rdbuf(fout.rdbuf());
#endif

    cin >> n;
    nod_terminal = 2 * n + 1;

    for (int i = 1; i <= n; i++) {
        gr[0].push_back(i);
        gr[i].push_back(0);
        capacitate[0][i] = 1;
    }

    for (int i = n + 1; i <= 2 * n ; i++) {
        gr[nod_terminal].push_back(i);
        gr[i].push_back(nod_terminal);
        capacitate[i][nod_terminal] = 1;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = n + 1; j <= 2 * n; j++) {
            int val; cin >> val;
            cost[i][j] = val;
            cost[j][i] = -val;
            gr[i].push_back(j);
            gr[j].push_back(i);
            capacitate[i][j] = 1;
        }
    }

    // aici urmeaza sa fac flux
    while (drum_ameliorare()) {
        int nod_curent = nod_terminal;

        while (nod_curent != 0) {
            int nod_anterior = tata[nod_curent];
            flux[nod_anterior][nod_curent] += 1;
            flux[nod_curent][nod_anterior] -= 1;
            nod_curent = nod_anterior;
        }

        // for (int i = 1; i <= n; i++) {
        //     for (int j = n + 1; j <= 2 * n; j++){
        //         if (flux[i][j]){
        //             cout << cost[i][j] << '(' << i << j << ") ";
        //         }
        //     }
        // }
        // cout << '\n';
    }

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = n + 1; j <= 2 * n; j++){
            if (flux[i][j]){
                ans += cost[i][j];
                //cout << cost[i][j] << ' ';
            }
        }
    }

    cout << ans;

    return 0;
}