2025-03-23 Daily Challenge

Today I have done leetcode's March LeetCoding Challenge with cpp.

March LeetCoding Challenge 23

Description

Number of Ways to Arrive at Destination

You are in a city that consists of n intersections numbered from 0 to n - 1 with bi-directional roads between some intersections. The inputs are generated such that you can reach any intersection from any other intersection and that there is at most one road between any two intersections.

You are given an integer n and a 2D integer array roads where roads[i] = [ui, vi, timei] means that there is a road between intersections ui and vi that takes timei minutes to travel. You want to know in how many ways you can travel from intersection 0 to intersection n - 1 in the shortest amount of time.

Return the number of ways you can arrive at your destination in the shortest amount of time. Since the answer may be large, return it modulo 109 + 7.

 

Example 1:

Input: n = 7, roads = [[0,6,7],[0,1,2],[1,2,3],[1,3,3],[6,3,3],[3,5,1],[6,5,1],[2,5,1],[0,4,5],[4,6,2]]
Output: 4
Explanation: The shortest amount of time it takes to go from intersection 0 to intersection 6 is 7 minutes.
The four ways to get there in 7 minutes are:
- 0 ➝ 6
- 0 ➝ 4 ➝ 6
- 0 ➝ 1 ➝ 2 ➝ 5 ➝ 6
- 0 ➝ 1 ➝ 3 ➝ 5 ➝ 6

Example 2:

Input: n = 2, roads = [[1,0,10]]
Output: 1
Explanation: There is only one way to go from intersection 0 to intersection 1, and it takes 10 minutes.

 

Constraints:

  • 1 <= n <= 200
  • n - 1 <= roads.length <= n * (n - 1) / 2
  • roads[i].length == 3
  • 0 <= ui, vi <= n - 1
  • 1 <= timei <= 109
  • ui != vi
  • There is at most one road connecting any two intersections.
  • You can reach any intersection from any other intersection.

Solution

class Solution {
  using pi = pair<int, int>;
  using plii = pair<long long, pi>;
  const int MOD = 1e9 + 7;
public:
  int countPaths(int n, vector<vector<int>>& roads) {
    vector<vector<pi>> neighbors(n);
    vector<int> count(n);
    vector<long long> distance(n, LONG_LONG_MAX);
    for(auto &r : roads) {
      neighbors[r[0]].push_back({r[1], r[2]});
      neighbors[r[1]].push_back({r[0], r[2]});
    }

    count[0] = 1;
    distance[0] = 0;
    priority_queue<plii, vector<plii>, greater<plii>> pq;
    pq.push({0, {-1, 0}});
    while(pq.size()) {
      auto [dist, fromTo] = pq.top();
      pq.pop();
      auto [from, current] = fromTo;
      bool visited = distance[current];
      if(distance[current] == LONG_LONG_MAX || (dist + current == 0)) {
        distance[current] = dist;
        for(auto [next, road] : neighbors[current]) {
          if(road + dist > distance[next]) continue;
          pq.push({road + dist, {current, next}});
        }
      }
      if(distance[current] == dist && from != -1) {
        count[current] += count[from];
        count[current] %= MOD;
      }
    }

    return count.back();
  }
};

// Accepted
// 55/55 cases passed (15 ms)
// Your runtime beats 34.92 % of cpp submissions
// Your memory usage beats 67.03 % of cpp submissions (36.8 MB)