Cod sursa(job #3361879)

Utilizator iuli_morariuIuli Morariu iuli_morariu Data 29 iulie 2026 12:27:58
Problema Cc Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.88 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <climits>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
// #include <bits/std++.h>
#define in  fin
#define out fout

using namespace std;
const int NMAX = 105;

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

// cc?? crying child?? fnaf reference??

struct muchie{
    int x, y, pereche, c, f, cost;
};

int n;
vector<muchie> mch;
vector<int> g[NMAX];
pair<int, int> last[NMAX];

void add_muchie(int x, int y, int c, int cost){
    int id = mch.size(), idp = mch.size() + 1;
    mch.push_back({x, y, idp, c, 0,  cost});
    mch.push_back({y, x, id,  0, 0, -cost});

    g[x].push_back(id);
    g[y].push_back(idp);
}

int the_real_ones[NMAX];
int perchance[NMAX]; // you can't just say perchance!!
int unc_still_got_it[NMAX]; // eu ieri la grave
int sursie, noelle;

void clopot_barbat_ford_vad(){
    for(int i = 1; i <= n; i++){
        unc_still_got_it[i] = INT_MAX;
    }
    unc_still_got_it[sursie] = 0;
    for(int i = 0; i < n; i++){
        for(const muchie &x : mch){
            if(unc_still_got_it[x.x] != INT_MAX && unc_still_got_it[x.x] + x.cost < unc_still_got_it[x.y]){
                unc_still_got_it[x.y] = unc_still_got_it[x.x] + x.cost;
            }
        }
    }
}

int flux = 0;
int gaster = 0;
// W D Gaster from deltarune
// it al comes from an empty room
// mike the cat yes that is gaster
// and the titan also gaster
// in the bunker sits that gaster
// entry 17's ol master
// kris is gaster
// susie gaster
// noelle gaster
// all are gaster
// the knight is an amalgamate
// made by gaster intricate
// gaster is actually green

bool va_rog_foarte_foarte_frumos(){
    priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
    for(int i = 1; i <= n; i++){
        perchance[i] = INT_MAX;
        last[i] = {0, -1};
    }

    perchance[sursie] = 0;
    the_real_ones[sursie] = 0;
    pq.push({0, sursie});

    while(!pq.empty()){
        int x = pq.top().second, d = pq.top().first;
        pq.pop();

        if(d != perchance[x]) continue;

        for(const int &id : g[x]){
            int y = mch[id].y;
            if(mch[id].c == mch[id].f) continue;

            int maybe = perchance[x] + mch[id].cost + unc_still_got_it[x] - unc_still_got_it[y];
            // maybe..... well meet at a bar.....
            // we'll drive.... a funky.... hughhhhh
            if(maybe < perchance[y]){
                last[y] = {x, id};
                perchance[y] = maybe;
                pq.push({maybe, y});
                the_real_ones[y] = the_real_ones[x] + mch[id].cost;
            }
        }
    }

    for(int i = 1; i <= n; i++){
        unc_still_got_it[i] = the_real_ones[i];
    }

    if(perchance[noelle] == INT_MAX){
        return 0;
    }

    int nod = noelle;
    int minim_ude = INT_MAX;
    while(nod != sursie){
        int id = last[nod].second;
        minim_ude = min(minim_ude, mch[id].c - mch[id].f);
        nod = last[nod].first;
    }

    gaster += minim_ude * the_real_ones[noelle];
    flux += minim_ude;

    nod = noelle;
    while(nod != sursie){
        int id = last[nod].second;
        mch[id].f += minim_ude;
        mch[ mch[id].pereche ].f -= minim_ude;
        nod = last[nod].first;
    }

    return 1;
}

int da_doame_sa_fi_scris_corect_din_prima(){
    clopot_barbat_ford_vad();
    while(va_rog_foarte_foarte_frumos());
    return gaster;
}

signed main(){
    ios_base::sync_with_stdio(false);
    in.tie(NULL);

    int ok; in >> ok;
    n = ok + ok + 2;
    sursie = 2 * ok + 1;
    noelle = sursie + 1; 

    for(int i = 1; i <= ok; i++){
        for(int j = 1; j <= ok; j++){
            int d; in >> d;
            add_muchie(i, j + ok, 1, d);
        }
    }

    for(int i = 1; i <= ok; i++){
        add_muchie(sursie, i, 1, 0);
        add_muchie(i + ok, noelle, 1, 0);
    }

    out << da_doame_sa_fi_scris_corect_din_prima() << '\n';

    return 0;
}