#P1364. 医院设置

    ID: 361 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划,dp树形结构广度优先搜索,BFS最短路

医院设置

Description

Given a binary tree as shown:

The number inside each circle is the population at that node. The number beside each circle is the node index. Choose one node to build a hospital so that the total distance traveled by all residents is minimized. The distance between two adjacent nodes is 11.

For example, if the hospital is built at node 11, the total distance is 4+12+2×20+2×40=1364 + 12 + 2 \times 20 + 2 \times 40 = 136. If it is built at node 33, the total distance is 4×2+13+20+40=814 \times 2 + 13 + 20 + 40 = 81.

Input Format

The first line contains an integer nn, the number of nodes in the tree.

Each of the next nn lines describes one node with three integers w,u,vw, u, v, where ww is the population at the node, uu is the left child (with 00 meaning no child), and vv is the right child (with 00 meaning no child).

Output Format

Output a single integer, the minimum total distance.

5						
13 2 3
4 0 0
12 4 5
20 0 0
40 0 0

81

Hint

Constraints

For 100% of the testdata, it is guaranteed that 1n1001 \leq n \leq 100, 0u,vn0 \leq u, v \leq n, 1w1051 \leq w \leq 10^5.

Translated by ChatGPT 5