CF BUDDY
← Problems·

1801D · The way home

2100 · binary search, data structures, dp

Problem: He's going to get there by plane. In total, there are mm flights in the country, ii-th flies from city aia_i to city bib_i and costs sis_i coins. Note that the ii-th flight is one-way, so it can't be used to get from city bib_i to city aia_i. To use it, Borya must be in the city aia_i and have at least sis_i coins (which he will spend on the flight).

After the robbery, he has only pp coins left, but he does not despair! Being in the city ii, he can organize performances every day, each performance will bring him wiw_i coins.

Help the magician find out if he will be able to get home, and what is the minimum number of performances he will have to organize.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t801 \le t \le 80) – the number of test cases. The description of test cases follows.

The first line contains three integers nn, mm and pp (2n8002 \le n \le 800, 1m30001 \le m \le 3000, 0p1090 \le p \le 10^9) — the number of cities, the number of flights and the initial amount of coins.

The second line contains nn integers w1,w2,,wnw_1, w_2, \ldots, w_n (1wi109)(1 \le w_i \le 10^9) — profit from representations.

The following mm lines each contain three integers aia_i, bib_i and sis_i (1ai,bin1 \le a_i, b_i \le n, 1si1091 \le s_i \le 10^9) — the starting and ending city, and the cost of ii-th flight.

It is guaranteed that the sum of nn over all test cases does not exceed 800800 and the sum of mm over all test cases does not exceed 1000010000.

Output Format: For each test case print a single integer — the minimum number of performances Borya will have to organize to get home, or 1-1 if it is impossible to do this.

Note: In the first example, it is optimal for Borya to make 44 performances in the first city, having as a result 2+74=302 + 7 \cdot 4 = 30 coins, and then walk along the route 13241-3-2-4, spending 6+8+11=256+8+11=25 coins. In the second example, it is optimal for Borya to make 1515 performances in the first city, fly to 33 city, make 99 performances there, and then go to 44 city.

Sample Cases

Case 1

Input

4
4 4 2
7 4 3 1
1 2 21
3 2 6
1 3 8
2 4 11
4 4 10
1 2 10 1
1 2 20
2 4 30
1 3 25
3 4 89
4 4 7
5 1 6 2
1 2 5
2 3 10
3 4 50
3 4 70
4 1 2
1 1 1 1
1 3 2

Output

4
24
10
-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback