← All problems

2699. Modify Graph Edge Weights

HardOpen on LeetCodeProblem statement

Problem Statement

2699. Modify Graph Edge Weights

Hard


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 * 109] 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:

Java

Source file
class Solution {
    private static final int INF = (int) 2e9;

    public int[][] modifiedGraphEdges(int n, int[][] edges, int source, int destination, int target) {
        List<int[]>[] adj = new List[n];
        for (int i = 0; i < n; i++)
            adj[i] = new ArrayList<>();
        for (int[] e : edges) {
            if (e[2] != -1) { // ignore negative edges so that Djikstra is applicable
                adj[e[0]].add(new int[] { e[1], e[2] });
                adj[e[1]].add(new int[] { e[0], e[2] });
            }
        }
        int currShortestPathLen = djikstra(adj, source, destination);
        if (currShortestPathLen < target) {
            // implying even if there were +ve edges instead of -ve -1s,
            // there would exist a shorter path. Hence, modifying -ve edges would be useless
            return new int[0][0];
        }
        boolean matchesTarget = (currShortestPathLen == target);
        for (int[] edge : edges) {
            if (edge[2] != -1)
                continue; // Skip edges with already known weights

            // Set edge weight to a large value if current distance matches target,
            // else set to minimum +ve value i.e., +1
            edge[2] = matchesTarget ? INF : 1;
            adj[edge[0]].add(new int[] { edge[1], edge[2] });
            adj[edge[1]].add(new int[] { edge[0], edge[2] });
            if (!matchesTarget) {
                // Compute the new shortest distance with the updated edge weight
                int newDistance = djikstra(adj, source, destination);
                if (newDistance <= target) {
                    matchesTarget = true;
                    edge[2] += target - newDistance;
                }
            }
        }
        // Return modified edges if the target distance is achieved,
        // otherwise return an empty result
        return matchesTarget ? edges : new int[0][0];
    }

    private int djikstra(List<int[]>[] adj,  int src, int dest) {
        int[] cost = new int[adj.length];
        Arrays.fill(cost, INF);
        Queue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]);
        pq.add(new int[] { src, 0 });
        while (!pq.isEmpty()) {
            int[] top = pq.poll();
            if (top[0] == dest)
                return top[1];
            for (int[] nbr : adj[top[0]]) {
                int newCost = top[1] + nbr[1];
                if (cost[nbr[0]] > newCost) {
                    cost[nbr[0]] = newCost;
                    pq.add(new int[] { nbr[0], newCost });
                }
            }
        }
        return INF;
    }
}