Naxocist

Journey

Apr 10th, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define INF 1e9
  4.  
  5. using pi = pair<int, int>;
  6.  
  7. const int N = 1e5 + 10;
  8.  
  9. vector<pi> adjList[N];
  10. int dist[N];
  11.  
  12. int main(){
  13.     int n, m, k; scanf("%d %d %d", &n, &m, &k);
  14.     for(int i=0; i<=n; ++i) dist[i] = INF;
  15.  
  16.     for(int i=0; i<m; ++i){
  17.         int u, v, w; scanf("%d %d %d", &u, &v, &w);
  18.         adjList[u].push_back({v, w});
  19.         adjList[v].push_back({u, w});
  20.     }
  21.  
  22.     priority_queue<pi, vector<pi>, greater<pi>> pq;
  23.    
  24.     pq.push({0, 1});
  25.     dist[1] = 0;
  26.     while(!pq.empty()){
  27.         int w = pq.top().first, u = pq.top().second;
  28.         pq.pop();
  29.  
  30.         if(w > dist[u]) continue;
  31.  
  32.         for(auto x : adjList[u]){
  33.             int v = x.first, vw = x.second;
  34.             if(w + vw < dist[v]){
  35.                 dist[v] = w + vw;
  36.                 pq.push({dist[v], v});
  37.             }
  38.         }
  39.     }
  40.  
  41.     int mx=1;
  42.     for(int i=1; i<=n; ++i) {
  43.         if(dist[i] == INF) continue;
  44.         if(dist[i] <= k){
  45.             mx = max(mx, i);
  46.         }
  47.     }
  48.  
  49.     printf("%d", mx);
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment