-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #763 from harshbhagwani94/patch-1
Leetcode1786.cpp
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Program's_Contributed_By_Contributors/C++ Programs/Leetcode1786.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//Leetcode-#1786. Number of Restricted Paths From First to Last Node - https://leetcode.com/problems/number-of-restricted-paths-from-first-to-last-node/ | ||
|
||
#define pii pair<int, int> | ||
#define mod 1000000007 | ||
class Solution { | ||
public: | ||
|
||
void minimumDistanceUsingDijkstra(vector<pii> graph[], vector<int> &distanceFromLastNode, int n) { | ||
priority_queue<pii, vector<pii>, greater<pii>> pq; | ||
pq.push({0,n}); | ||
distanceFromLastNode[n] = 0; | ||
while(!pq.empty()) { | ||
int u = pq.top().second; | ||
pq.pop(); | ||
for (auto edge : graph[u]) { | ||
int v = edge.first, dis = edge.second; | ||
if (distanceFromLastNode[v] > distanceFromLastNode[u] + dis) { | ||
distanceFromLastNode[v] = distanceFromLastNode[u] + dis; | ||
pq.push({distanceFromLastNode[v], v}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
int dfs(vector<int> &path, vector<pii> graph[], vector<int> &distanceFromLastNode, int src) { | ||
if (src==1) | ||
return 1; | ||
|
||
if(path[src]!=-1) | ||
return path[src]; | ||
|
||
int ans=0; | ||
|
||
for (auto edge : graph[src]) { | ||
int v = edge.first; | ||
if (distanceFromLastNode[v] > distanceFromLastNode[src]) { | ||
ans = (ans%mod + dfs(path, graph, distanceFromLastNode, v))%mod; | ||
} | ||
} | ||
|
||
return path[src]=ans; | ||
} | ||
|
||
int countRestrictedPaths(int n, vector<vector<int>>& edges) { | ||
vector<pii> graph[n+1]; | ||
vector<int> distanceFromLastNode(n+1, INT_MAX); | ||
for(auto edge : edges) { | ||
graph[edge[0]].push_back({edge[1], edge[2]}); | ||
graph[edge[1]].push_back({edge[0], edge[2]}); | ||
} | ||
minimumDistanceUsingDijkstra(graph, distanceFromLastNode, n); | ||
vector<int> path(n+1, -1); | ||
return dfs(path, graph, distanceFromLastNode, n ); | ||
} | ||
|
||
}; |