Cod sursa(job #3197771)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 27 ianuarie 2024 13:57:33
Problema Pitici Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

using namespace std;
ifstream fin("pitici.in");
ofstream fout("pitici.out");

struct cv {
    short node;
    int cost;
    bool operator<(const cv& foreign) const {
        return this->cost > foreign.cost;
    }
};

short N,K;
int M;
vector<unordered_map<short,short>> G;

int main() {
    fin >> N >> M >> K;
    G.resize(N+1);
    for (int i = 1; i <= M; i++) {
        short nodeA, nodeB, cost;
        fin >> nodeA >> nodeB >> cost;
        G[nodeA][nodeB] = cost;
    }
    priority_queue<cv> pq,ans;
    for (const pair<short,short>& edge : G[1]) {
        pq.push({edge.first, edge.second});
    }
    G[1].clear();
    while (!pq.empty()) {
        if (pq.top().node == N) {
            ans.push(pq.top());
            if (ans.size() == K) {
                break;
            }
        } else {
            for (const pair<short,short>& edge : G[pq.top().node]) {
                pq.push({edge.first, edge.second + pq.top().cost});
            }
        }
        pq.pop();
    }
    while (K--) {
        fout << ans.top().cost << ' ';
        ans.pop();
    }
    return 0;
}