Cod sursa(job #3190051)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 6 ianuarie 2024 21:08:41
Problema Cc Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.32 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 flux_pe_nod[nmax];
int tata[nmax];
int nod_terminal;

vector<int> gr[nmax];
queue<int> q;

//FUNCTIONS

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

    tata[0] = -2;
    drum[0] = 0;
    flux_pe_nod[0] = inf;
    while (!q.empty()) q.pop();
    q.push(0);

    while (!q.empty()) {
        int nod_curent = q.front();
        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;
                drum[nod_adiacent] = drum[nod_curent] + cost[nod_curent][nod_adiacent];
                flux_pe_nod[nod_adiacent] = min(flux_pe_nod[nod_curent], capacitate[nod_curent][nod_adiacent] - flux[nod_curent][nod_adiacent]);
                if (nod_adiacent == nod_terminal) return flux_pe_nod[nod_adiacent];
                q.push(nod_adiacent);
            }
        }
    }


    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;

    // citirea e ok
    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
    int flux_total = 0;
    int flux_nou;
    while (flux_nou = drum_ameliorare()){
        flux_total += flux_nou;
        int nod_curent = nod_terminal;

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

        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;
}