Minimize the Total Price of the Trips Problem
Description
LeetCode Problem 2646.
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] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.
Each node has an associated price. You are given an integer array price, where price[i] is the price of the i^th node.
The price sum of a given path is the sum of the prices of all nodes lying on that path.
Additionally, you are given a 2D integer array trips, where trips[i] = [start_i, end_i] indicates that you start the i^th trip from the node start_i and travel to the node end_i by any path you like.
Before performing your first trip, you can choose some non-adjacent nodes and halve the prices.
Return the minimum total price sum to perform all the given trips.
Example 1:

Input: n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]
Output: 23
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.
For the 1^st trip, we choose path [0,1,3]. The price sum of that path is 1 + 2 + 3 = 6.
For the 2^nd trip, we choose path [2,1]. The price sum of that path is 2 + 5 = 7.
For the 3^rd trip, we choose path [2,1,3]. The price sum of that path is 5 + 2 + 3 = 10.
The total price sum of all trips is 6 + 7 + 10 = 23.
It can be proven, that 23 is the minimum answer that we can achieve.
Example 2:

Input: n = 2, edges = [[0,1]], price = [2,2], trips = [[0,0]]
Output: 1
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.
For the 1^st trip, we choose path [0]. The price sum of that path is 1.
The total price sum of all trips is 1. It can be proven, that 1 is the minimum answer that we can achieve.
Constraints:
1 <= n <= 50edges.length == n - 10 <= a_i, b_i <= n - 1edgesrepresents a valid tree.price.length == nprice[i]is an even integer.1 <= price[i] <= 10001 <= trips.length <= 1000 <= start_i, end_i <= n - 1
Solution
Approach: Tree DP
First count how many trips pass through each node. dfs(i, fa, k) walks from i towards the target k, adds 1 to cnt for every node it enters and undoes that when k is not in the subtree, so afterwards cnt[i] is the number of trips that visit node i.
Then a second search dfs2(i, fa) returns two numbers for the subtree of i: the cheapest total when i keeps its price (a) and when i is halved (b). Start with a = cnt[i] * price[i] and b = a / 2. For each child with result (x, y): if i keeps its price the child may do either, so a += min(x, y); if i is halved the child must keep its price, so b += x. The answer is min(a, b) at the root.
Complexity: O(m * n + n) time, O(n) space, where m is the number of trips and n the number of nodes.
class Solution:
def minimumTotalPrice(
self, n: int, edges: List[List[int]], price: List[int], trips: List[List[int]]
) -> int:
def dfs(i: int, fa: int, k: int) -> bool:
cnt[i] += 1
if i == k:
return True
ok = any(j != fa and dfs(j, i, k) for j in g[i])
if not ok:
cnt[i] -= 1
return ok
def dfs2(i: int, fa: int) -> (int, int):
a = cnt[i] * price[i]
b = a // 2
for j in g[i]:
if j != fa:
x, y = dfs2(j, i)
a += min(x, y)
b += x
return a, b
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
cnt = Counter()
for start, end in trips:
dfs(start, -1, end)
return min(dfs2(0, -1))