CSES - Shared codeLink to this code:
https://cses.fi/paste/a41c757040f58fd717cc05/
/*
------------____________----------------
--------------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));
}
ll dist[n+1][k];
ffor(i,0,n+1)
ffor(j,0,k)
dist[i][j] = INF;
set<PIII> PQ;
dist[1][0] = 0;
PQ.insert(mp(0,mp(1,0)));
while(PQ.size()>0)
{
PIII P = *PQ.begin();
ll weight = P.ff , currVertex = P.ss.ff , whichCheapest = P.ss.ss;
//cout<<weight<<" "<<currVertex<<" "<<whichCheapest<<endl;
PQ.erase(P);
for(PII curr : adjList[currVertex])
{
ll nextVertex = curr.ff , edgeWeight = curr.ss;
ffor(i,0,k)
{
if(weight+edgeWeight < dist[nextVertex][i])
{
dist[nextVertex][k-1] = weight+edgeWeight;
PQ.insert(mp(weight+edgeWeight , mp(nextVertex,i)));
sort(dist[nextVertex]+i ,dist[nextVertex]+k);
break;
}
}
}
}
ffor(i,0,k)
cout<<dist[n][i]<<" ";
}