A12193 | The Fair Nut and the Best Path
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The Fair Nut is going to travel to the Tree Country, in which there are $n$ cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city $u$ and go by a simple path to city $v$ . He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex.
A filling station is located in every city. Because of strange law, Nut can buy only $w_i$ liters of gasoline in the $i$ -th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.
He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.
A filling station is located in every city. Because of strange law, Nut can buy only $w_i$ liters of gasoline in the $i$ -th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last.
He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it.
输入格式
The first line contains a single integer $n$ ( $1 \leq n \leq 3 \cdot 10^5$ ) — the number of cities.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ( $0 \leq w_{i} \leq 10^9$ ) — the maximum amounts of liters of gasoline that Nut can buy in cities.
Each of the next $n - 1$ lines describes road and contains three integers $u$ , $v$ , $c$ ( $1 \leq u, v \leq n$ , $1 \leq c \leq 10^9$ , $u \ne v$ ), where $u$ and $v$ — cities that are connected by this road and $c$ — its length.
It is guaranteed that graph of road connectivity is a tree.
The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ( $0 \leq w_{i} \leq 10^9$ ) — the maximum amounts of liters of gasoline that Nut can buy in cities.
Each of the next $n - 1$ lines describes road and contains three integers $u$ , $v$ , $c$ ( $1 \leq u, v \leq n$ , $1 \leq c \leq 10^9$ , $u \ne v$ ), where $u$ and $v$ — cities that are connected by this road and $c$ — its length.
It is guaranteed that graph of road connectivity is a tree.
输出格式
Print one number — the maximum amount of gasoline that he can have at the end of the path.
输入输出样例
输入 #1
3 1 3 3 1 2 2 1 3 2
输出 #1
3
输入 #2
5 6 3 2 5 0 1 2 10 2 3 3 2 4 1 1 5 1
输出 #2
7
The optimal way in the first example is $2 \to 1 \to 3$ .
The optimal way in the second example is $2 \to 4$ .

The optimal way in the second example is $2 \to 4$ .

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted