#P15814. [JOI 2014 Final] フクロモモンガ

[JOI 2014 Final] フクロモモンガ

Problem Description

In the forest where the wombat JOI lives, there are NN eucalyptus trees, numbered from 11 to NN. The height of tree ii is HiH_i meters.

There are MM 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 11 meter per second. That is, if JOI’s current height above the ground is hh meters, and it takes tt seconds to jump between two trees, then his height above the ground after the jump will be h−th - t meters. However, if h−th - t is less than 00 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 00 meters to the height of the tree he is currently on. It takes 11 second for JOI to increase or decrease his height above the ground by 11 meter.

JOI wants to go from a position on tree 11 at height XX meters above the ground to the top of tree NN (that is, the position at height HNH_N 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 NN.

Input Format

Read the following data from standard input.

  • The first line contains three space-separated integers N,M,XN, M, X. This means there are NN trees, MM pairs of trees that can be jumped between, and initially JOI is on tree 11 at height XX meters above the ground.
  • In the next NN lines, the ii-th line (1≤i≤N1 \le i \le N) contains one integer HiH_i, meaning that tree ii has height HiH_i meters.
  • In the next MM lines, the jj-th line (1≤j≤M1 \le j \le M) contains three space-separated integers Aj,Bj,TjA_j, B_j, T_j (1≤Aj≤N1 \le A_j \le N, 1≤Bj≤N1 \le B_j \le N, Aj≠BjA_j \ne B_j). This means JOI can jump in both directions between tree AjA_j and tree BjB_j, and the required time is TjT_j seconds. Also, for 1≤j<k≤M1 \le j < k \le M, it holds that (Aj,Bj)≠(Ak,Bk)(A_j, B_j) \ne (A_k, B_k) and (Aj,Bj)≠(Bk,Ak)(A_j, B_j) \ne (B_k, A_k).

Output Format

Output one line to standard output containing one integer: the minimum time (in seconds) needed to reach the top of tree NN starting from the position on tree 11 at height XX meters above the ground. If it is impossible to reach, output −1-1.

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:

  1. Climb up 5050 meters on tree 1.
  2. Jump from tree 1 to tree 2.
  3. Jump from tree 2 to tree 4.
  4. Jump from tree 4 to tree 5.
  5. Climb up 1010 meters on tree 5.

Sample Explanation 2

JOI cannot jump from tree 1 to tree 2.

Constraints

All input data satisfy the following conditions.

  • 2≤N≤1000002 \le N \le 100000
  • 1≤M≤3000001 \le M \le 300000
  • 1≤Hi≤10000000001 \le H_i \le 1000000000 (1≤i≤N1 \le i \le N)
  • 1≤Tj≤10000000001 \le T_j \le 1000000000 (1≤j≤M1 \le j \le M)
  • 0≤X≤H10 \le X \le H_1

Subtasks

Subtask 1 [25 points]

The following conditions are satisfied.

  • N≤1000N \le 1000
  • M≤3000M \le 3000
  • Hi≤100H_i \le 100 (1≤i≤N1 \le i \le N)
  • Tj≤100T_j \le 100 (1≤j≤M1 \le j \le M)

Subtask 2 [25 points]

The following condition is satisfied.

  • X=0X = 0

Subtask 3 [50 points]

There are no additional constraints.


Translated by DeepSeek V3.2.

Translated by ChatGPT 5