diff --git a/1976. Number of Ways to Arrive at Destination b/1976. Number of Ways to Arrive at Destination new file mode 100644 index 0000000..e9e7a12 --- /dev/null +++ b/1976. Number of Ways to Arrive at Destination @@ -0,0 +1,45 @@ +class Solution { +public: + vector>>graph; + + int countPaths(int n, vector>& roads) { + + graph.resize(n); + + for(vectorroad : roads){ + int u = road[0], v = road[1], time = road[2]; + graph[u].push_back({v,time}); + graph[v].push_back({u,time}); + } + + vectordist(n, LLONG_MAX); + vectorways(n, 0); + priority_queue, vector>, greater<>> pq; + + dist[0] = 0; + ways[0] = 1; + pq.push({0, 0}); + + while(!pq.empty()){ + auto [currTime, node] = pq.top(); + pq.pop(); + + if(currTime > dist[node]){ + continue; + } + + for(auto [nextNode, time] : graph[node]){ + long long newTime = time + currTime; + if(newTime < dist[nextNode]){ + dist[nextNode] = newTime; + ways[nextNode] = ways[node]; + pq.push({newTime, nextNode}); + }else if(newTime == dist[nextNode]){ + ways[nextNode] = (ways[nextNode] + ways[node]) % 1000000007; + } + } + } + + return ways[n - 1]; + } +};