Skip to content

Latest commit

 

History

History

1976

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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.

Companies:
Facebook

Related Topics:
Dynamic Programming, Graph, Topological Sort, Shortest Path

Similar Questions:

Solution 1. Dijkstra

Intuition: Since we are looking for the shortest path in a weighted graph, we can use Dijkstra.

Algorithm: We just need to add one more array cnt such that cnt[i] is the number of ways to go from node 0 to node i with the shortest distance.

If we find a new path from u to v with a total cost c that is:

  • < dist[v], we do dist[v] = c, cnt[v] = cnt[u], and push (c, v) into the heap.
  • == dist[v], we do cnt[v] += cnt[u]. We don't need to push (c, v) into the heap because it must be pushed already.
  • > dist[v], we can ignore this path.
// OJ: https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/
// Author: github.com/lzl124631x
// Time: O(ElogE)
// Space: O(V + E)
class Solution {
    typedef pair<long, long> PII;
public:
    int countPaths(int n, vector<vector<int>>& E) {
        vector<vector<pair<int, int>>> G(n);
        for (auto &e : E) {
            int u = e[0], v = e[1], t = e[2];
            G[u].emplace_back(v, t);
            G[v].emplace_back(u, t);
        }
        long mod = 1e9 + 7;
        vector<long> dist(n, LONG_MAX), cnt(n);
        priority_queue<PII, vector<PII>, greater<>> pq; // time, city
        dist[0] = 0;
        cnt[0] = 1;
        pq.emplace(0, 0);
        while (pq.size()) {
            auto [cost, u] = pq.top();
            pq.pop();
            if (cost > dist[u]) continue;
            for (auto &[v, time] : G[u]) {
                long c = cost + time;
                if (c < dist[v]) {
                    dist[v] = c;
                    cnt[v] = cnt[u];
                    pq.emplace(c, v);
                } else if (c == dist[v]) cnt[v] = (cnt[v] + cnt[u]) % mod;
            }
        }
        return cnt[n - 1];
    }
};