Shortest Route with Free Tolls

hard ☼ daily · +3 ◆ graphs shortest-paths heap 2000 ms · 256 MB

Shortest Route with Free Tolls

You are given a directed graph with n vertices and m edges. Traversing edge i costs w_i.

You also hold k vouchers. Each time you traverse an edge you may hand in one voucher, and then that traversal costs nothing. A voucher is used up once handed in; if the same edge is traversed twice, each traversal needs its own voucher to be free. You do not have to use all your vouchers.

Find the minimum total cost of a walk from vertex 1 to vertex n.

Input

The first line contains three integers n, m and k (1 ≤ n ≤ 10^4, 0 ≤ m ≤ 5·10^4, 0 ≤ k ≤ 10).

Each of the next m lines contains three integers u_i, v_i and w_i (1 ≤ u_i, v_i ≤ n, 1 ≤ w_i ≤ 10^9): a directed edge from u_i to v_i with cost w_i. The graph may contain self-loops and several edges between the same pair of vertices.

Output

Print one integer: the minimum total cost of a walk from vertex 1 to vertex n, or -1 if vertex n cannot be reached from vertex 1.

Notes

In the first example, the cheapest route without vouchers is 1 → 2 → 4 with cost 7. With one voucher, the route 1 → 3 → 4 costs only 1 when the edge 1 → 3 (cost 10) is free.

In the second example, the plain cheapest route 1 → 2 → 3 → 4 costs 6, and making its two most expensive edges free leaves a cost of 1. But the direct edge 1 → 4 can be made free on its own, so the answer is 0.

If n = 1, you are already at the destination and the answer is 0.

Example 1

Input

4 5 1
1 2 3
2 4 4
1 3 10
3 4 1
2 3 5

Output

1

Example 2

Input

4 4 2
1 2 1
2 3 2
3 4 3
1 4 100

Output

0

Hints · reveal one at a time if you're stuck

Hint 1

Taking the cheapest plain route and freeing its most expensive edges does not work. Whether you can still use a voucher depends on how many you have already spent, so what extra piece of information must the search remember at each vertex?

Hint 2

Make the state a pair: a vertex together with the number of vouchers spent so far, from 0 up to k. From a state, paying for an edge keeps the voucher count, while spending a voucher on an edge moves to the next count at cost 0.

Hint 3

All costs are non-negative, so a heap-based shortest path search works on this larger state graph, and you never need to build it explicitly. The answer is the best distance to vertex n over all voucher counts, and the distances can exceed 32 bits.

report an issue Prefer your own editor? sooks prep 41 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.