← Microsoft Interview Insights
I knew Dijkstra immediately but blanked on what to do after.
First, compute the shortest distance from node n to all other nodes using Dijkstra's algorithm. Then, sort nodes by distance from n in ascending order and use dynamic programming to count the number of restricted paths from node 1 to each node, where transitions are only allowed from a node to its neighbors that are strictly closer to n. Finally, output the count for node n modulo 10^9+7.
Pro tip: Clarify that the graph is undirected and weighted, and that 'strictly closer' means the shortest distance to n decreases at each step. Mention that the DP must process nodes in increasing order of distance from n to ensure correct dependencies.
Run Dijkstra's algorithm from node n to find the shortest distance d[v] from every node v to n. This defines the 'closer' relation.
Create a list of nodes sorted in ascending order of d[v]. This order ensures that when processing a node, all nodes closer to n have already been processed.
Set dp[1] = 1 (one way to start at node 1) and dp[v] = 0 for all other nodes. dp[v] will store the number of restricted paths from 1 to v.
For each node u in sorted order, for each neighbor v of u, if d[v] < d[u], then add dp[u] to dp[v] modulo 10^9+7. This counts paths that move strictly closer to n.
After processing all nodes, dp[n] contains the total number of restricted paths from 1 to n. Output it modulo 10^9+7.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.