CSES - Shared codeLink to this code:
https://cses.fi/paste/1265dbc8786d4f5e17cd08/
/*
------------____________----------------
--------------CRISTIANO-----------------
SUUUUUUUUUIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
GOOOOOOOOOOOOOOAAAAAAAALLLLLLLLLLLLLLLLL
Author- Shubham Jha
IIIT- ALLAHABAD
*/
/* MAX in O(logN) using Sparse Tables */
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 998244353
#define ffor(i,a,b) for(int i=a;i<b;i++)
#define bfor(i,a,b) for(int i=a-1;i>=b;i--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define endl "\n"
#define INF 1010100100000000
#define MAXN 1000005
#define PII pair<ll,ll>
#define PIII pair< ll , pair<ll,ll> >
#define PIIII pair<ll, PIII>
int main()
{
fast;
ll n , m ,k , a , b, w;
cin>>n>>m>>k;
vector<PII> adjList[n+1];
ffor(i,0,m)
{
cin>>a>>b>>w;
adjList[a].pb(mp(b,w));
}
priority_queue<ll> individualSorted[n+1];
priority_queue<PII , vector<PII> , greater<PII> > PQ;
ffor(i,0,n+1)
ffor(j,0,k)
individualSorted[i].push(INF);
individualSorted[1].pop();
individualSorted[1].push(0);
PQ.push(mp(0,1));
while(PQ.size()>0)
{
PII P = PQ.top();
PQ.pop();
ll weight = P.ff , currVertex = P.ss;
for(PII curr : adjList[currVertex])
{
ll nextVertex = curr.ff , edgeWeight = curr.ss;
ll maximumCost = individualSorted[nextVertex].top();
if(weight+edgeWeight < maximumCost)
{
individualSorted[nextVertex].pop();
individualSorted[nextVertex].push(weight+edgeWeight);
PQ.push(mp(weight+edgeWeight ,nextVertex));
}
}
}
vector<ll>v ;
while(individualSorted[n].size()>0)
{
v.pb(individualSorted[n].top());
individualSorted[n].pop();
}
sort(all(v));
ffor(i,0,k)
cout<<v[i]<<" ";
}