A1380 | [COCI-2020-2021-contest2]#4 Sjekira
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
Mirko is tired of his stressful CEO job in a well-known multinational software company. With a golden parachute of several million dollars, he decided to live a simple life and move to Gorski Kotar. However, winters in the remote village he just moved in are tough. None of his neighbours were born after WWII, so he is destined to chop firewood himself.
Today, he is going to chop his first trunk. Before chopping, he labels the parts of the trunk which are small enough to fit in a fireplace, and measures their hardness.
Mirko is a programmer, so he notices that the parts and the connections between them form a tree graph.
The damage on his axe resulting from cutting a connection on the trunk is equal to the sum of the maximal hardnesses in the two connected components formed by cutting the connection.
Mirko has only one axe, so he wants the total damage to be as small as possible. He wants to know the minimal total damage on the axe, if he cuts the whole trunk into single parts which fit in a fireplace.
Today, he is going to chop his first trunk. Before chopping, he labels the parts of the trunk which are small enough to fit in a fireplace, and measures their hardness.
Mirko is a programmer, so he notices that the parts and the connections between them form a tree graph.
The damage on his axe resulting from cutting a connection on the trunk is equal to the sum of the maximal hardnesses in the two connected components formed by cutting the connection.
Mirko has only one axe, so he wants the total damage to be as small as possible. He wants to know the minimal total damage on the axe, if he cuts the whole trunk into single parts which fit in a fireplace.
输入格式
The first line contains an integer n, the number of parts. The parts are labeled by integers from 1 to n.
The second line contains n integers ti (1 ≤ ti ≤ 109 ). The number ti is the hardness of the part labeled i.
Each of the following n − 1 lines contains two integers x and y (1 ≤ x, y ≤ n) – labels of parts that are directly connected.
The second line contains n integers ti (1 ≤ ti ≤ 109 ). The number ti is the hardness of the part labeled i.
Each of the following n − 1 lines contains two integers x and y (1 ≤ x, y ≤ n) – labels of parts that are directly connected.
输出格式
Output the minimal total damage after n − 1 cuts.
输入输出样例
输入 #1
3 1 2 3 1 2 2 3
输出 #1
8
输入 #2
4 2 2 3 2 1 3 3 2 4 3
输出 #2
15
Clarification of the first example:
There are two ways to cut this trunk. He can first cut the connection (1, 2), which causes 1 + 3 = 4
damage, and then cut the connection (2, 3), which causes 2 + 3 = 5 damage. The total damage is 9 in this
case.
Otherwise, he can first cut (2, 3), and then (1, 2). The total damage in that case (2 + 3) + (1 + 2) = 8.
There are two ways to cut this trunk. He can first cut the connection (1, 2), which causes 1 + 3 = 4
damage, and then cut the connection (2, 3), which causes 2 + 3 = 5 damage. The total damage is 9 in this
case.
Otherwise, he can first cut (2, 3), and then (1, 2). The total damage in that case (2 + 3) + (1 + 2) = 8.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted