MAGCARI

Budget Travelling

Sep 7th, 2022
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. /*
  2.     Task        : Budget Travelling
  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;
  10.     bool operator < (const A&o) const{
  11.         if(w!=o.w)  return w>o.w;
  12.         else        return v>o.v;
  13.     }
  14. };
  15. struct B{
  16.     int v,w;
  17. };
  18. priority_queue<A > heap;
  19. vector<B > g[10010];
  20. int dis[10010],dis2[10010];
  21. int main(){
  22.     int n,m,st,en,k,u,v,w;
  23.     scanf("%d %d %d %d %d",&n,&m,&st,&en,&k);
  24.     for(int i=1;i<=m;i++){
  25.         scanf("%d %d %d",&u,&v,&w);
  26.         g[u].push_back({v,w});
  27.         g[v].push_back({u,w});
  28.     }
  29.     for(int i=0;i<n;i++)
  30.         dis[i] = dis2[i] = 1e9;
  31.    
  32.     dis[st] = 0;
  33.     heap.push({st,0});
  34.     while(!heap.empty()){
  35.         A now = heap.top();
  36.         heap.pop();
  37.         if(now.v == en){
  38.             printf("%d %d 0\n",en,now.w);
  39.             return 0;
  40.         }
  41.         for(auto x:g[now.v]){
  42.             if(x.w+now.w <= k && dis[x.v] > x.w+now.w){
  43.                 dis[x.v] = x.w+now.w;
  44.                 heap.push({x.v,dis[x.v]});
  45.             }
  46.         }
  47.     }
  48.  
  49.     dis2[en] = 0;
  50.     heap.push({en,0});
  51.     while(!heap.empty()){
  52.         A now = heap.top();
  53.         heap.pop();
  54.         if(dis[now.v] != 1e9){
  55.             printf("%d %d %d",now.v,dis[now.v],dis2[now.v]);
  56.             return 0;
  57.         }
  58.         for(auto x:g[now.v]){
  59.             if(dis2[x.v] > x.w+now.w){
  60.                 dis2[x.v] = x.w+now.w;
  61.                 heap.push({x.v,dis2[x.v]});
  62.             }
  63.         }
  64.     }
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment