2646. Minimize the Total Price of the Trips Hard
1/**
2 * [2646] Minimize the Total Price of the Trips
3 *
4 * There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.
5 * Each node has an associated price. You are given an integer array price, where price[i] is the price of the i^th node.
6 * The price sum of a given path is the sum of the prices of all nodes lying on that path.
7 * Additionally, you are given a 2D integer array trips, where trips[i] = [starti, endi] indicates that you start the i^th trip from the node starti and travel to the node endi by any path you like.
8 * Before performing your first trip, you can choose some non-adjacent nodes and halve the prices.
9 * Return the minimum total price sum to perform all the given trips.
10 *
11 * <strong class="example">Example 1:
12 * <img alt="" src="https://assets.leetcode.com/uploads/2023/03/16/diagram2.png" style="width: 541px; height: 181px;" />
13 * Input: n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]
14 * Output: 23
15 * Explanation: The diagram above denotes the tree after rooting it at node 2. The first part shows the initial tree and the second part shows the tree after choosing nodes 0, 2, and 3, and making their price half.
16 * For the 1^st trip, we choose path [0,1,3]. The price sum of that path is 1 + 2 + 3 = 6.
17 * For the 2^nd trip, we choose path [2,1]. The price sum of that path is 2 + 5 = 7.
18 * For the 3^rd trip, we choose path [2,1,3]. The price sum of that path is 5 + 2 + 3 = 10.
19 * The total price sum of all trips is 6 + 7 + 10 = 23.
20 * It can be proven, that 23 is the minimum answer that we can achieve.
21 *
22 * <strong class="example">Example 2:
23 * <img alt="" src="https://assets.leetcode.com/uploads/2023/03/16/diagram3.png" style="width: 456px; height: 111px;" />
24 * Input: n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]
25 * Output: 1
26 * Explanation: The diagram above denotes the tree after rooting it at node 0. The first part shows the initial tree and the second part shows the tree after choosing node 0, and making its price half.
27 * For the 1^st trip, we choose path [0]. The price sum of that path is 1.
28 * The total price sum of all trips is 1. It can be proven, that 1 is the minimum answer that we can achieve.
29 *
30 *
31 * Constraints:
32 *
33 * 1 <= n <= 50
34 * edges.length == n - 1
35 * 0 <= ai, bi <= n - 1
36 * edges represents a valid tree.
37 * price.length == n
38 * price[i] is an even integer.
39 * 1 <= price[i] <= 1000
40 * 1 <= trips.length <= 100
41 * 0 <= starti, endi <= n - 1
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimize-the-total-price-of-the-trips/
47// discuss: https://leetcode.com/problems/minimize-the-total-price-of-the-trips/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn minimum_total_price(n: i32, edges: Vec<Vec<i32>>, price: Vec<i32>, trips: Vec<Vec<i32>>) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_2646() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.