MAGCARI

Dijkstra K state

Aug 23rd, 2022
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. /*
  2.     Task        : dijkstra
  3.     Author      : Phumipat C. [MAGCARI]
  4.     Language    : C++
  5. */
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8. struct A{
  9.     int v,w,k;
  10.     bool operator < (const A&o) const{
  11.         return w>o.w;
  12.     }
  13. };
  14. priority_queue<A > heap;
  15. vector<A > g[100010];
  16. int dis[100010][110];
  17. int main(){
  18.     int n,m,u,v,w,lim;
  19.     cin >> n >> m >> lim;
  20.     for(int i=1;i<=m;i++){
  21.         scanf("%d %d %d",&u,&v,&w);
  22.         g[u].push_back({v,w});
  23.         g[v].push_back({u,w});
  24.     }
  25.     for(int i=1;i<=n;i++)
  26.         for(int j=0;j<=100;j++)
  27.             dis[i][j] = 1e9;
  28.     dis[1][0] = 0;
  29.     heap.push({1,0,0});
  30.     while(!heap.empty()){
  31.         A now = heap.top();
  32.         heap.pop();
  33.         for(auto x:g[now.v]){
  34.  
  35.             if(now.k+1<=lim && dis[x.v][now.k+1] > now.w){
  36.                 dis[x.v][now.k+1] = now.w;
  37.                 heap.push({x.v,now.w,now.k+1});
  38.             }
  39.  
  40.             if(dis[x.v][now.k] > now.w + x.w){
  41.                 dis[x.v][now.k] = now.w + x.w;
  42.                 heap.push({x.v,now.w+x.w,now.k});
  43.             }
  44.         }
  45.     }
  46.     printf("%d\n",dis[n]);
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment