Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ar array<int,3>
- int main(){
- int n,m,k;
- cin>>n>>m>>k;
- vector<vector<pair<int, int>>> adj(n);
- for(int i=0;i<m;i++){
- int u, v, w;
- cin>>u>>v>>w;
- adj[u-1].push_back({v-1, w});
- adj[v-1].push_back({u-1, w});
- }
- vector<vector<int>> dp(n, vector<int>(k + 1, INT_MAX)); //dp[i][j] -> 0 to i with atmost j skips
- priority_queue<ar, vector<ar>, greater<ar>> q; // {dist,node,used}
- q.push({0, 0, 0});
- dp[0][0] = 0 ;
- while(!q.empty()){
- auto [d,n,next] = q.top();
- q.pop();
- if(d!=dp[n][next]) continue;
- if(n==0) dp[n][next]=0;
- for(auto [v,w]:adj[n]){
- if(dp[n][next]+w<dp[v][next]){
- dp[v][next] = dp[n][next]+w;
- q.push({dp[v][next],v,next});
- }
- if(next == k) continue;
- if(dp[n][next]<dp[v][next+1]){
- dp[v][next+1] = dp[n][next];
- q.push({dp[v][next+1],v,next+1});
- }
- }
- }
- for(int i = 0;i < n;i++){
- cout<<dp[i][k]<<" ";
- }
- cout<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement