Under construction

Still being built. Things may change

Trips

Time limit2sMemory limit512 MB

Summary
Find the length of the k-th shortest walk in a directed graph with road lengths 1 to 3, where walks may reuse roads and towns, or print -1 if fewer than k exist.
Level

Medium7 of 10

Topics
Matrix, Binary search, Graph
Solved
No attempts yet

Problem

Byteasar has recently discovered the joy of bicycle trips. He plans to spend his kk days of vacation cycling through the countryside of Byteotia. Each day he wants to take a trip along a different route. He also wants the trips to get harder over time, so each trip must be no shorter than the one before it. Specifically, on day ii he follows the ii-th shortest possible route that passes through the towns of Byteotia. Find the length of the last trip, which he takes on day kk.

Input

The first line contains three integers nn, mm, and kk separated by single spaces, where 1≤n≤401 \le n \le 40, 1≤m≤10001 \le m \le 1000, and 1≤k≤10181 \le k \le 10^{18}. The next mm lines describe the roads, one per line. Each line contains three integers uu, vv, and cc separated by single spaces, where 1≤u,v≤n1 \le u, v \le n, u≠vu \ne v, and 1≤c≤31 \le c \le 3. This means there is a one-way road from town uu to town vv with length cc kilometers. Multiple roads may connect the same pair of towns. Routes may start and end in any towns, and they may pass through the same town or road more than once.

In tests worth 75% of the total score, n≤15n \le 15, m≤200m \le 200, and k≤1012k \le 10^{12}. In a subset worth 50% of the total score, c=1c = 1 for every road. In a subset of that worth 25% of the total score, k≤1 000 000k \le 1\,000\,000.

Output

Print the length of the kk-th shortest route on a single line. If there are fewer than kk different routes, print −1-1.

Hint

Routes of length 1: 1 → 2, 5 → 3, 4 → 5.

Routes of length 2: 2 → 3, 3 → 4, 4 → 5 → 3.

Routes of length 3: 4 → 6, 1 → 2 → 3, 3 → 4 → 5, 5 → 3 → 4.

One possible eleventh shortest route (of length 4) is 5 → 3 → 4 → 5.

Examples1

  1. Example 1

    Input
    6 6 11
    1 2 1
    2 3 2
    3 4 2
    4 5 1
    5 3 1
    4 6 3
    
    Expected output
    4