A14476 | Labyrinth
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
In a dream, Lucy found herself in a labyrinth. This labyrinth consists of $n$ rooms, connected by $m$ passages ( $i$ -th passage is $w_i$ cm wide). Each passage can be traversed in both directions. It is guaranteed that it is possible to get from any room to any other room. But this is not an ordinary labyrinth — each room in this labyrinth contains a magic candy. When Lucy eats this magic candy, she is getting wider. Specifically, if she eats candy from room $i$ she becomes wider by $c_i$ cm. Note that she is not obliged to eat candy the first time she visits a particular room, but she can eat each candy only once.
Unfortunately, passages in this labyrinth are pretty narrow, so after eating some candy, Lucy can get too wide and will not be able to traverse them — her width should not be greater than the width of the corresponding passage.
Lucy starts her journey in a room number $1$ . She wants to eat all the candies. After that, she will just wake up, so she does not have to be able to return to the room $1$ . She realizes that with her current width, she may not be able to do so, so she plans a workout before embarking on her journey. Lucy wants to know if it is possible to start with some positive width and still eat all the candies. If yes, then what is the maximal starting width with which it is possible.
Unfortunately, passages in this labyrinth are pretty narrow, so after eating some candy, Lucy can get too wide and will not be able to traverse them — her width should not be greater than the width of the corresponding passage.
Lucy starts her journey in a room number $1$ . She wants to eat all the candies. After that, she will just wake up, so she does not have to be able to return to the room $1$ . She realizes that with her current width, she may not be able to do so, so she plans a workout before embarking on her journey. Lucy wants to know if it is possible to start with some positive width and still eat all the candies. If yes, then what is the maximal starting width with which it is possible.
输入格式
The first line contains two integers, $n$ and $m$ ( $2 \le n \le 10^5; n - 1 \le m \le 10^5$ ) — the number of rooms and the number of passages.
The second line contains $n$ integers — $c_i$ ( $1 \le c_i \le 10^9$ ).
Next $m$ lines contain three integers each — $a_i$ , $b_i$ and $w_i$ ( $1 \le a_i, b_i \le n; a_i \ne b_i; 1 \le w_i \le 10^9$ ) describing passage that connects rooms $a_i$ and $b_i$ and is $w_i$ cm wide. It is guaranteed that the resulting labyrinth is connected and there is at most one passage between any pair of rooms.
The second line contains $n$ integers — $c_i$ ( $1 \le c_i \le 10^9$ ).
Next $m$ lines contain three integers each — $a_i$ , $b_i$ and $w_i$ ( $1 \le a_i, b_i \le n; a_i \ne b_i; 1 \le w_i \le 10^9$ ) describing passage that connects rooms $a_i$ and $b_i$ and is $w_i$ cm wide. It is guaranteed that the resulting labyrinth is connected and there is at most one passage between any pair of rooms.
输出格式
If it is possible to eat all the candies, output the maximal possible starting width, otherwise output $-1$ .
输入输出样例
输入 #1
3 3 1 2 3 1 2 4 1 3 4 2 3 6
输出 #1
3
输入 #2
2 1 1 1 1 2 1
输出 #2
-1
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted