#P2726. [SHOI2005] 树的双中心
[SHOI2005] 树的双中心
Description
Given a tree , where is the set of nodes and is the set of edges.
For each node , there is a weight function whose values are positive integers.
Let be the distance between nodes and , meaning the number of edges on the unique path between them. If and are the same node, then .
Your task is to find two distinct nodes and that minimize the following expression:
$$S(x,y)=\sum_{v\in V} (W(v)\cdot \min\{ d(v,x),d(v,y)\})$$Input Format
- The first line contains (), the number of nodes in the tree. The nodes are numbered from to .
- The next lines each contain two integers , indicating there is an edge between and .
- Then more lines follow. Each line contains a positive integer; on the -th of these lines, the integer gives the weight of node .
- It is guaranteed that the depth of the tree does not exceed .
Output Format
Output the minimal value of . The result is guaranteed not to exceed .
5
1 2
1 3
3 4
3 5
5
7
6
5
4
14
Hint
In the sample, the two chosen centers are nodes and .
Translated by ChatGPT 5
京公网安备 11011102002149号