Advertisement
Slayerfeed

Journey

Apr 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <vector>
  4. #include <queue>
  5. #include <utility>
  6. using namespace std;
  7.  
  8. long long int n ,m ,k;
  9.  
  10. using pr = pair<long long int, long long int>;
  11. long long int INF = 2e9;
  12. vector<pr> g[100010];
  13.  
  14.  
  15. int main(){
  16.  
  17.  
  18.     cin >> n >> m >> k;
  19.     long long int u1 ,v1 ,d1;
  20.     for(int i=0;i<m;++i){
  21.         cin >> u1 >> v1 >> d1;
  22.         g[u1].push_back(make_pair(v1,d1));
  23.         g[v1].push_back(make_pair(u1,d1));
  24.     }
  25.     vector <long long int> dist(n+1,INF);
  26.     vector <bool> visit(n+1,false);
  27.  
  28.     priority_queue<pr, vector<pr>, greater<pr>> q;
  29.  
  30.     dist[1]=0;
  31.     q.push({dist[1],1});
  32.     long long int max1=1;
  33.  
  34.     while(!q.empty()){
  35.         long long int u = q.top().second , d = q.top().first;
  36.  
  37.         q.pop();
  38.  
  39.         if(visit[u]){
  40.             continue;
  41.         }
  42.  
  43.         visit[u] = true;
  44.         if(dist[u]<=k && max1<u){
  45.  
  46.             max1 =u;
  47.         }
  48.         for(auto c : g[u]){
  49.             long long int x = c.first;
  50.             long long int y = c.second;
  51.  
  52.             if(!visit[x]&&dist[u]+y < dist[x]){
  53.                 dist[x]=dist[u]+y;
  54.                 q.push({dist[x],x});
  55.             }
  56.         }
  57.     }
  58.  
  59.  
  60.  
  61.     cout  << max1;
  62.  
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement