2699. Modify Graph Edge Weights Hard
1/**
2 * [2699] Modify Graph Edge Weights
3 *
4 * 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.
5 * Some edges have a weight of -1 (wi = -1), while others have a positive weight (wi > 0).
6 * 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.
7 * 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.
8 * Note: You are not allowed to modify the weights of edges with initial positive weights.
9 *
10 * <strong class="example">Example 1:
11 * <strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph.png" style="width: 300px; height: 300px;" />
12 *
13 * Input: n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
14 * Output: [[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
15 * Explanation: The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5.
16 *
17 * <strong class="example">Example 2:
18 * <strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph-2.png" style="width: 300px; height: 300px;" />
19 *
20 * Input: n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
21 * Output: []
22 * 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.
23 *
24 * <strong class="example">Example 3:
25 * <strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/graph-3.png" style="width: 300px; height: 300px;" />
26 *
27 * Input: n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
28 * Output: [[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
29 * Explanation: The graph above shows a modified graph having the shortest distance from 0 to 2 as 6.
30 *
31 *
32 * Constraints:
33 *
34 * 1 <= n <= 100
35 * <font face="monospace">1 <= edges.length <= n * (n - 1) / 2</font>
36 * edges[i].length == 3
37 * 0 <= ai, bi < n
38 * <font face="monospace">wi = -1 </font>or <font face="monospace">1 <= wi <= 10^<span style="font-size: 10.8333px;">7</span></font>
39 * ai != bi
40 * 0 <= source, destination < n
41 * source != destination
42 * <font face="monospace">1 <= target <= 10^9</font>
43 * The graph is connected, and there are no self-loops or repeated edges
44 *
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/modify-graph-edge-weights/
49// discuss: https://leetcode.com/problems/modify-graph-edge-weights/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54 pub fn modified_graph_edges(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32, target: i32) -> Vec<Vec<i32>> {
55 vec![]
56 }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_2699() {
67 }
68}
69
Back
© 2025 bowen.ge All Rights Reserved.