Cod sursa(job #3197763)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 27 ianuarie 2024 13:38:42
Problema Pitici Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>

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

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

int N,M,K;

vector<unordered_map<int,int>> G;
vector<bool> viz;

int main() {
    fin >> N >> M >> K;
    viz.assign(N+1,false);
    G.resize(N+1);
    for (int i = 1; i <= M; i++) {
        int nodeA, nodeB, cost;
        fin >> nodeA >> nodeB >> cost;
        G[nodeA][nodeB] = cost;
    }
    priority_queue<cv> pq,ans;
    for (const pair<int,int>& edge : G[1]) {
        pq.push({edge.first, edge.second});
    }
    viz[N] = true;
    while (!pq.empty()) {
        if (pq.top().node == N) {
            ans.push(pq.top());
            if (ans.size() == K) {
                break;
            }
        } else {
            viz[pq.top().node] = true;
            for (const pair<int,int>& 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;
}