-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1976_Leetcode.cpp
39 lines (32 loc) · 1.07 KB
/
1976_Leetcode.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
class Solution {
const int MOD = 1e9 + 7;
public:
int countPaths(int n, vector<vector<int>>& roads) {
vector<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]});
}
vector<long long> dist(n, LLONG_MAX);
vector<int> ways(n, 0);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
dist[0] = 0;
ways[0] = 1;
pq.push({0, 0});
while(!pq.empty()){
auto [currDist, u] = pq.top(); pq.pop();
if(currDist > dist[u]) continue;
for(auto& [v, wt] : adj[u]){
long long newDist = currDist + wt;
if(newDist < dist[v]){
dist[v] = newDist;
ways[v] = ways[u];
pq.push({newDist, v});
} else if(newDist == dist[v]){
ways[v] = (ways[v] + ways[u]) % MOD;
}
}
}
return ways[n-1];
}
};