Minimize shortest path by adding weight-1 edges
Problem Description
You are given a weighted directed graph with n nodes, labeled from 1 to n. The existing edges are defined by three arrays of equal length m: from[i], to[i], and weight[i], representing a directed edge from from[i] to to[i] with a positive weight weight[i].
You have the ability to add any number of additional directed edges between any two nodes in the graph. Each newly added edge has a fixed weight of 1.
Your task is to determine the minimum possible shortest-path distance from node 1 to node n after optimally adding these weight-1 edges.
Input
n: The number of nodes in the graph.from: An array of start nodes for existing edges.to: An array of end nodes for existing edges.weight: An array of positive weights for existing edges.
Output
An integer representing the smallest achievable shortest-path distance from node 1 to node n.
Constraints
1 <= n <= 2e51 <= m <= 2e51 <= weight[i] <= 1e9
AI interview prep
Core dilemma
Efficiently finding the shortest path in a graph containing both arbitrarily large positive weighted edges and unit-weighted edges, without explicitly constructing an O(N^2) graph of all possible unit-weight additions.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Minimize shortest path by adding weight-1 edges
Problem Description
You are given a weighted directed graph with n nodes, labeled from 1 to n. The existing edges are defined by three arrays of equal length m: from[i], to[i], and weight[i], representing a directed edge from from[i] to to[i] with a positive weight weight[i].
You have the ability to add any number of additional directed edges between any two nodes in the graph. Each newly added edge has a fixed weight of 1.
Your task is to determine the minimum possible shortest-path distance from node 1 to node n after optimally adding these weight-1 edges.
Input
n: The number of nodes in the graph.from: An array of start nodes for existing edges.to: An array of end nodes for existing edges.weight: An array of positive weights for existing edges.
Output
An integer representing the smallest achievable shortest-path distance from node 1 to node n.
Constraints
1 <= n <= 2e51 <= m <= 2e51 <= weight[i] <= 1e9