Minimize shortest path by adding weight-1 edges
MathWorksSoftware Engineer · Technical ScreenLast updated
This question evaluates proficiency in graph algorithms, shortest-path computation, and algorithmic optimization when augmenting a directed weighted graph with additional unit-weight edges.
You are given a weighted directed graph with nodes labeled 1..n.
The existing edges are described by three arrays of equal length m:
from[i]: start node of edgeito[i]: end node of edgeiweight[i]: positive weight of edgei
You are allowed to add any number of additional directed edges, where each added edge has weight = 1, and you may choose its endpoints (any nodes in 1..n).
After adding edges optimally, compute the minimum possible shortest-path distance from node 1 to node n.
Input
n: number of nodesfrom[0..m-1],to[0..m-1],weight[0..m-1]
Output
- An integer: the smallest achievable distance from
1tonafter adding weight-1 edges.
Notes / Constraints (reasonable for interviews)
1 <= n <= 2e51 <= m <= 2e51 <= weight[i] <= 1e9
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.