This page is still under construction.

Parts of this page are still being built. What you see may change.

Empire

Interview

Time limit1sMemory limit256 MB

Summary
Find the fastest route from A to B whose total hull damage stays strictly below K.
Level

Medium5 of 10

Topics
Shortest path, Dynamic programming
Solved
No attempts yet

Problem

Jiyong tore up a calculus textbook and built a raft of thickness KK, thick enough to carry one person. He now plans to leave port A and sail to the uninhabited island B to found his empire.

The sea holds NN islands and MM sea routes. Nothing outside a sea route is passable, so Jiyong sails from island to island. Sea route ii takes time tit_i to cross and shaves hih_i centimeters off the raft.

Once the total hih_i of the crossed routes reaches KK, the raft falls to a thickness of 0cm or less and Jiyong, who cannot swim, does not survive. The voyage is safe only while the total hih_i of the crossed routes stays below KK.

Find the safe voyage from A to B that takes the least time and print that time. He may pass through the same island or the same sea route more than once, and every crossing adds its time and its damage again.

Input

The first line has three integers KK, NN, MM. (1≤K≤2001 \le K \le 200, 2≤N≤20002 \le N \le 2000, 1≤M≤100001 \le M \le 10000)

Each of the next MM lines describes one sea route as uu, vv, tit_i, hih_i. (1≤u,v≤N1 \le u, v \le N, u≠vu \ne v, 1≤ti≤1000001 \le t_i \le 100000, 0≤hi≤2000 \le h_i \le 200) A two-way sea route connects island uu and island vv, crossing it takes time tit_i, and it shaves hih_i off the raft. Several sea routes may connect the same pair of islands.

The last line has the start AA and the destination BB. (1≤A,B≤N1 \le A, B \le N, A≠BA \ne B)

Output

Print the minimum time of a safe voyage from A to B, or -1 if Jiyong cannot sail there safely.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    7
    
  2. Example 2

    Input
    3 3 3
    1 2 5 1
    3 2 8 2
    1 3 1 3
    1 3
    
    Expected output
    -1