# 1976. Number of Ways to Arrive at Destination

## Problem Statement

\


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`.

&#x20;

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/07/17/graph2.png)

<pre><code><strong>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]]
</strong><strong>Output: 4
</strong><strong>Explanation: The shortest amount of time it takes to go from intersection 0 to intersection 6 is 7 minutes.
</strong>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
</code></pre>

**Example 2:**

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

&#x20;

**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.

## Intuition

```
Declare a ways array to keep track of number of paths reaching that
Point and add all the paths in the end
```

### Links

[https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/description/](https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/description/)

### Video Links

[https://www.youtube.com/watch?v=\_-0mx0SmYxA\&ab\_channel=takeUforward](https://www.youtube.com/watch?v=_-0mx0SmYxA\&ab_channel=takeUforward)

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
typedef long long ll;
typedef pair<long,long> pii;

class Solution {
public:
    int countPaths(int n, vector<vector<int>>& roads) {
        vector<ll> distance(n, 1e15);
        vector<ll> ways(n,0);
        ways[0] = 1;
        distance[0] = 0;
        ll mod = (1e9+7);

        vector<vector<ll>> adj[n];
        for(auto &it: roads){
            ll x=it[0], y=it[1], dist=it[2];
            adj[x].push_back({y, dist});
            adj[y].push_back({x, dist});
        }

        priority_queue<pii, vector<pii>, greater<pii>> pq;
        pq.push({0, 0});

        while(!pq.empty()){
            ll dist=pq.top().first, node=pq.top().second;
            pq.pop();

            for(auto &it: adj[node]){
                ll neigh=it[0], cost=it[1];

                if(cost+dist < distance[neigh]){
                    distance[neigh] = cost+dist;
                    ways[neigh] = ways[node]%mod;
                    pq.push({distance[neigh], neigh});
                } 
                else if(cost+dist == distance[neigh]){
                    ways[neigh] = ((ways[neigh]%mod) + (ways[node]%mod))%mod;
                }
            }
        }

        return ways[n-1]%mod;
    }
};
```
{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Similar Problems

###

