Modify Graph Edge Weights - Problem

You are given an undirected weighted connected graph containing n nodes labeled from 0 to n - 1, and an integer array edges where edges[i] = [ai, bi, wi] indicates that there is an edge between nodes ai and bi with weight wi.

Some edges have a weight of -1 (wi = -1), while others have a positive weight (wi > 0).

Your task is to modify all edges with a weight of -1 by assigning them positive integer values in the range [1, 2 * 10⁹] so that the shortest distance between the nodes source and destination becomes equal to an integer target. If there are multiple modifications that make the shortest distance between source and destination equal to target, any of them will be considered correct.

Return an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from source to destination equal to target, or an empty array if it's impossible.

Note: You are not allowed to modify the weights of edges with initial positive weights.

Input & Output

Example 1 — Basic Graph with One -1 Edge
$ Input: n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
Output: [[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
💡 Note: Set the unknown edge weights so that the shortest path from node 0 to node 1 equals exactly 5. One possible solution is to set edge (0,3) to weight 3, keeping others at minimum weight 1.
Example 2 — Impossible Target
$ Input: n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
Output: []
💡 Note: There's a direct edge from 0 to 2 with weight 5, so the shortest path is already 5. We cannot make it longer to reach target 6.
Example 3 — Multiple -1 Edges
$ Input: n = 4, edges = [[1,0,4],[1,2,-1],[2,3,-1]], source = 0, destination = 3, target = 7
Output: [[1,0,4],[1,2,1],[2,3,2]]
💡 Note: Path 0→1→2→3 with weights 4+1+2=7. Set edge (1,2) to 1 and (2,3) to 2 to achieve target distance.

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ edges.length ≤ n × (n - 1) / 2
  • edges[i].length == 3
  • 0 ≤ ai, bi < n
  • wi = -1 or 1 ≤ wi ≤ 107
  • 0 ≤ source, destination < n
  • source ≠ destination
  • 1 ≤ target ≤ 2 × 109
  • The graph is connected

Visualization

Tap to expand
INPUT GRAPH0123w = -1w = 4w = 1w = 1Source: 0, Dest: 1Target Distance: 5ALGORITHM STEPS1Set all -1 edges to weight 1Run Dijkstra: min distance = 62Set all -1 edges to 2×10⁹Run Dijkstra: max distance = 2×10⁹3Binary search on edge weightRange [1, 2×10⁹] → converge to 24Verify solutionEdge (0,1) weight = 2, distance = 5 ✓Time: O(log(2×10⁹) × (V+E) log V)Space: O(V + E)FINAL RESULT0123w = 2w = 4w = 1w = 1Shortest Path: 0→1Distance = 2 = Target ✓Key Insight:Use Dijkstra twice to find distance bounds, then binary search on critical edge weight.This efficiently finds the exact weight needed to achieve target distance.TutorialsPoint - Modify Graph Edge Weights | Dijkstra + Binary Search
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28 Apple 22
23.5K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen