-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberOfWaysToReachDestination.cpp
48 lines (40 loc) · 1.41 KB
/
NumberOfWaysToReachDestination.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
int countPaths(int n, vector<vector<int>>& roads) {
int mod = (1e9 + 7);
vector<pair<int,int>> adj[n];
for(auto it: roads){
adj[it[0]].push_back({it[1], it[2]});
adj[it[1]].push_back({it[0], it[2]});
}
priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> pq;
vector<long long> dist(n, 1e18);
vector<int> ways(n, 0);
dist[0] = 0;
ways[0] = 1;
pq.push({0,0});
while(!pq.empty()){
long long dis = pq.top().first;
int node = pq.top().second;
pq.pop();
for(auto i : adj[node]){
int adjnode = i.first;
long long adjweight = i.second;
if(dis + adjweight < dist[adjnode]){
dist[adjnode] = adjweight + dis;
pq.push({dist[adjnode], adjnode});
// IMP
ways[adjnode] = ways[node]% mod;
//
}
else if(dis + adjweight == dist[adjnode])
{
// IMP
ways[adjnode] = (ways[adjnode] + ways[node])%mod;
//
}
}
}
return (ways[n - 1]) % mod;
}
};