Cod sursa(job #3206162)

Utilizator susanEmily Susan susan Data 21 februarie 2024 19:10:37
Problema Pitici Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <queue>
#include <vector>
#include <cstdint>

using namespace std;
using edge = pair<uint16_t, uint16_t>;
using state = pair<uint16_t, int>;


int main() {
    ifstream f("pitici.in");
    ofstream g("pitici.out");

    int n, m, k, a, b, cost;
    f >> n >> m >> k;

    auto comp = [](state &a, state &b) {
        if (a.second == b.second)
            return a.first < b.first;
        return a.second > b.second;
    };

    vector<vector<edge>> G(n + 1);

    while (m--) {
        f >> a >> b >> cost;
        G[b].emplace_back(a, cost);
    }

    priority_queue<state, vector<state>, decltype(comp)> Q(comp);

    for (auto &[nod, tot]: G[n])
        Q.emplace(nod, int(tot));

    while (!Q.empty()) {
        auto [nod, tot] = Q.top();
        Q.pop();

        if (nod == 1) {
            g << tot << ' ';
            if (--k == 0)
                break;
        } else
            for (auto &[nod2, pret]: G[nod])
                Q.emplace(nod2, tot + int(pret));
    }
}