Modify Graph Edge Weights Problem
Description
LeetCode Problem 2699.
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] = [a_i, b_i, w_i] indicates that there is an edge between nodes a_i and b_i with weight w_i.
Some edges have a weight of -1 (w_i = -1), while others have a positive weight (w_i > 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^9] 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.
Example 1:

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]]
Explanation: The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5.
Example 2:

Input: n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
Output: []
Explanation: The graph above contains the initial edges. It is not possible to make the distance from 0 to 2 equal to 6 by modifying the edge with weight -1. So, an empty array is returned.
Example 3:

Input: n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
Output: [[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
Explanation: The graph above shows a modified graph having the shortest distance from 0 to 2 as 6.
Constraints:
1 <= n <= 1001 <= edges.length <= n * (n - 1) / 2edges[i].length == 30 <= a_i, b_i< nw_i = -1or1 <= w_i<= 10^7a_i!= b_i0 <= source, destination < nsource != destination1 <= target <= 10^9- The graph is connected, and there are no self-loops or repeated edges
Solution
Approach: Shortest Path (Dijkstra's Algorithm)
First, we ignore the edges with a weight of -1 and use Dijkstra's algorithm to find the shortest distance d from source to destination.
If
d < target, it means there is a shortest path composed entirely of positive weight edges. No matter how we modify the edges with a weight of-1, we cannot make the shortest distance fromsourcetodestinationequal totarget. Therefore, there is no modification scheme that satisfies the problem, and we can return an empty array.If
d = target, it means there is a shortest path composed entirely of positive weight edges, and its length istarget. In this case, we can modify all edges with a weight of-1to the maximum value 2 × 109.If
d > target, we can try to add an edge with a weight of-1to the graph, set the weight of the edge to1, and then use Dijkstra's algorithm again to find the shortest distancedfromsourcetodestination.- If the shortest distance
d <= target, it means that after adding this edge, the shortest path can be shortened, and the shortest path must pass through this edge. Then we only need to change the weight of this edge totarget - d + 1to make the shortest path equal totarget. Then we can modify the remaining edges with a weight of-1to the maximum value 2 × 109. - If the shortest distance
d > target, it means that after adding this edge, the shortest path will not be shortened. Then we greedily keep the weight of this edge as-1and continue to try to add the remaining edges with a weight of-1.
- If the shortest distance
Complexity: O(E n2) time (up to one Dijkstra run per edge with an unknown weight) and O(n2) space, where n is the number of nodes.
class Solution:
def modifiedGraphEdges(
self, n: int, edges: List[List[int]], source: int, destination: int, target: int
) -> List[List[int]]:
def dijkstra(edges: List[List[int]]) -> int:
g = [[inf] * n for _ in range(n)]
for a, b, w in edges:
if w == -1:
continue
g[a][b] = g[b][a] = w
dist = [inf] * n
dist[source] = 0
vis = [False] * n
for _ in range(n):
k = -1
for j in range(n):
if not vis[j] and (k == -1 or dist[k] > dist[j]):
k = j
vis[k] = True
for j in range(n):
dist[j] = min(dist[j], dist[k] + g[k][j])
return dist[destination]
inf = 2 * 10**9
d = dijkstra(edges)
if d < target:
return []
ok = d == target
for e in edges:
if e[2] > 0:
continue
if ok:
e[2] = inf
continue
e[2] = 1
d = dijkstra(edges)
if d <= target:
ok = True
e[2] += target - d
return edges if ok else []