2065. Maximum Path Quality of a Graph Hard
1/**
2 * [2065] Maximum Path Quality of a Graph
3 *
4 * There is an undirected graph with n nodes numbered from 0 to n - 1 (inclusive). You are given a 0-indexed integer array values where values[i] is the value of the i^th node. You are also given a 0-indexed 2D integer array edges, where each edges[j] = [uj, vj, timej] indicates that there is an undirected edge between the nodes uj and vj, and it takes timej seconds to travel between the two nodes. Finally, you are given an integer maxTime.
5 * A valid path in the graph is any path that starts at node 0, ends at node 0, and takes at most maxTime seconds to complete. You may visit the same node multiple times. The quality of a valid path is the sum of the values of the unique nodes visited in the path (each node's value is added at most once to the sum).
6 * Return the maximum quality of a valid path.
7 * Note: There are at most four edges connected to each node.
8 *
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/19/ex1drawio.png" style="width: 269px; height: 170px;" />
11 * Input: values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
12 * Output: 75
13 * Explanation:
14 * One possible path is 0 -> 1 -> 0 -> 3 -> 0. The total time taken is 10 + 10 + 10 + 10 = 40 <= 49.
15 * The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.
16 *
17 * Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/19/ex2drawio.png" style="width: 269px; height: 170px;" />
19 * Input: values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
20 * Output: 25
21 * Explanation:
22 * One possible path is 0 -> 3 -> 0. The total time taken is 10 + 10 = 20 <= 30.
23 * The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.
24 *
25 * Example 3:
26 * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/19/ex31drawio.png" style="width: 236px; height: 170px;" />
27 * Input: values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50
28 * Output: 7
29 * Explanation:
30 * One possible path is 0 -> 1 -> 3 -> 1 -> 0. The total time taken is 10 + 13 + 13 + 10 = 46 <= 50.
31 * The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.
32 *
33 *
34 * Constraints:
35 *
36 * n == values.length
37 * 1 <= n <= 1000
38 * 0 <= values[i] <= 10^8
39 * 0 <= edges.length <= 2000
40 * edges[j].length == 3
41 * 0 <= uj < vj <= n - 1
42 * 10 <= timej, maxTime <= 100
43 * All the pairs [uj, vj] are unique.
44 * There are at most four edges connected to each node.
45 * The graph may not be connected.
46 *
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/maximum-path-quality-of-a-graph/
51// discuss: https://leetcode.com/problems/maximum-path-quality-of-a-graph/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56 pub fn maximal_path_quality(values: Vec<i32>, edges: Vec<Vec<i32>>, max_time: i32) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_2065() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.