Guest User

Untitled

a guest
Nov 11th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. int countPaths(int n, vector<vector<int>>& roads) {
  2.         vector<vector<pair<int, int>>> adjList(n);
  3.         for (auto it : roads) {
  4.             adjList[it[0]].push_back({it[1], it[2]});
  5.             adjList[it[1]].push_back({it[0], it[2]});
  6.         }
  7.         priority_queue<
  8.             pair<long long, long long>,
  9.             vector<pair<long long, long long>>,
  10.             greater<pair<long, long>>
  11.         > pq;
  12.         pq.push({0, 0});
  13.  
  14.         int shortestTime = INT_MAX;
  15.         int shortestPaths = 0;
  16.         int mod = 1e9 + 7;
  17.  
  18.         vector<long long> dist(n,1e18);
  19.         dist[0] = 0;
  20.  
  21.         while(!pq.empty()) {
  22.             auto top = pq.top();
  23.             pq.pop();
  24.  
  25.             if(top.second == n-1) {
  26.                 if(shortestTime > top.first) {
  27.                     shortestTime = top.first;
  28.                     shortestPaths = 1;
  29.                 } else if (shortestTime == top.first) {
  30.                     shortestPaths++;
  31.                     shortestPaths = shortestPaths % mod;
  32.                 }
  33.             }
  34.  
  35.             for(auto& it: adjList[top.second]) {
  36.                 long long distance = top.first + it.second;
  37.                 if(distance <= dist[it.first]) {
  38.                     pq.push({distance, it.first});
  39.                     dist[it.first] = distance;
  40.                 }
  41.             }
  42.         }
  43.  
  44.         return shortestPaths;
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment