Trips
Time limit2sMemory limit512 MB
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 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 he follows the -th shortest possible route that passes through the towns of Byteotia. Find the length of the last trip, which he takes on day .
Input
The first line contains three integers , , and separated by single spaces, where , , and . The next lines describe the roads, one per line. Each line contains three integers , , and separated by single spaces, where , , and . This means there is a one-way road from town to town with length 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, , , and . In a subset worth 50% of the total score, for every road. In a subset of that worth 25% of the total score, .
Output
Print the length of the -th shortest route on a single line. If there are fewer than different routes, print .
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.
