#P15814. [JOI 2014 Final] フクロモモンガ
[JOI 2014 Final] フクロモモンガ
Problem Description
In the forest where the wombat JOI lives, there are eucalyptus trees, numbered from to . The height of tree is meters.
There are pairs of trees between which JOI can jump directly, and the time needed to jump between each such pair is fixed. While JOI is jumping between trees, his height above the ground decreases at a rate of meter per second. That is, if JOI’s current height above the ground is meters, and it takes seconds to jump between two trees, then his height above the ground after the jump will be meters. However, if is less than or greater than the height of the destination tree, then this jump cannot be performed.
In addition, JOI can move up and down along the side of a tree to adjust his height above the ground within the range from meters to the height of the tree he is currently on. It takes second for JOI to increase or decrease his height above the ground by meter.
JOI wants to go from a position on tree at height meters above the ground to the top of tree (that is, the position at height meters above the ground). He wants to know the minimum time needed to achieve this.
Task
Given the height of each tree, the information of pairs of trees between which JOI can jump directly, and JOI’s initial height, write a program to find the minimum time needed to reach the top of tree .
Input Format
Read the following data from standard input.
- The first line contains three space-separated integers . This means there are trees, pairs of trees that can be jumped between, and initially JOI is on tree at height meters above the ground.
- In the next lines, the -th line () contains one integer , meaning that tree has height meters.
- In the next lines, the -th line () contains three space-separated integers (, , ). This means JOI can jump in both directions between tree and tree , and the required time is seconds. Also, for , it holds that and .
Output Format
Output one line to standard output containing one integer: the minimum time (in seconds) needed to reach the top of tree starting from the position on tree at height meters above the ground. If it is impossible to reach, output .
5 5 0
50
100
25
30
10
1 2 10
2 5 50
2 4 20
4 3 1
5 4 20
110
2 1 0
1
1
1 2 100
-1
4 3 30
50
10
20
50
1 2 10
2 3 10
3 4 10
100
Hint
Sample Explanation 1
For example, you can move in the following way:
- Climb up meters on tree 1.
- Jump from tree 1 to tree 2.
- Jump from tree 2 to tree 4.
- Jump from tree 4 to tree 5.
- Climb up meters on tree 5.
Sample Explanation 2
JOI cannot jump from tree 1 to tree 2.
Constraints
All input data satisfy the following conditions.
- ()
- ()
Subtasks
Subtask 1 [25 points]
The following conditions are satisfied.
- ()
- ()
Subtask 2 [25 points]
The following condition is satisfied.
Subtask 3 [50 points]
There are no additional constraints.
Translated by DeepSeek V3.2.
Translated by ChatGPT 5