CCCGuide

Problem S4: Convex Hull

2015 · S4 · Original problem · DMOJ

Problem Description

You are travelling on a ship in an archipelago. The ship has a convex hull which is KK centimetres thick. The archipelago has NN islands, numbered from 11 to NN. There are MM sea routes amongst them, where the iith route runs directly between two different islands aia_i and bi (1≤ai,bi≤N)b_i~(1 \leq a_i,b_i \leq N), takes tit_i minutes to travel along in either direction, and has rocks that wear down the ship’s hull by hih_i centimetres. There may be multiple routes running between a pair of islands.

You would like to travel from island AA to a different island B (1≤A,B≤N)B~(1 \leq A,B \leq N) along a sequence of sea routes, such that your ship’s hull remains intact – in other words, such that the sum of the routes’ hih_i values is strictly less than KK.

Additionally, you are in a hurry, so you would like to minimize the amount of time necessary to reach island BB from island AA. It may not be possible to reach island BB from island AA, however, either due to insufficient sea routes or the having the ship’s hull wear out.

Input Specification

The first line of input contains three integers KK, NN and M (1≤K≤200,2≤N≤2000,1≤M≤10000)M~(1 \leq K \leq 200, 2 \leq N \leq 2000, 1 \leq M \leq 10000), each separated by one space.

The next MM lines each contain 4 integers aia_i bib_i tit_i and hi (1≤ai,bi≤N,1≤ti≤105,0≤hi≤200)h_i~(1 \leq a_i, b_i \leq N, 1 \leq t_i \leq 10^5, 0 \leq h_i \leq 200), each separated by one space. The iith line in this set of MM lines describes the iith sea route (which runs from island aia_i to island bib_i, takes tit_i minutes and wears down the ship’s hull by hih_i centimetres). Notice that ai≠bia_i \not= b_i (that is, the ends of a sea route are distinct islands).

The last line of input contains two integers AA and B (1≤A,B≤N;A≠B)B~(1 \leq A,B \leq N; A \not= B), the islands between which we want to travel.

For 20% of marks for this question, K=1K=1 and N≤200N \leq 200. For another 20% of the marks for this problem, K=1K=1 and N≤2000N \leq 2000.

Output Specification

Output a single integer: the integer representing the minimal time required to travel from AA to BB without wearing out the ship’s hull, or −1-1 to indicate that there is no way to travel from AA to BB without wearing out the ship’s hull.

Sample Input 1

10 4 7
1 2 4 4
1 3 7 2
3 1 8 1
3 2 2 2
4 2 1 6
3 4 1 1
1 4 6 12
1 4

Output for Sample Input 1

7

Explanation of Output for Sample Input 1

The path of length 1 from 11 to 44 would wear out the hull of the ship. The three paths of length 22 ([1,2,4][1,2,4] and [1,3,4][1,3,4] two different ways) take at least 8 minutes. The path [1,2,3,4][1,2,3,4] takes 7 minutes and only wears down the hull by 7 centimetres, whereas the path [1,3,2,4][1,3,2,4] takes 1313 minutes and wears down the hull by 5 centimetres.

Sample Input 2

3 3 3
1 2 5 1
3 2 8 2
1 3 1 3
1 3

Output for Sample Input 2

-1

Explanation of Output for Sample Input 2

The direct path [1,3][1,3] wears down the hull to 00, as does the path [1,2,3][1,2,3].