#P9751. [CSP-J 2023] 旅游巴士
[CSP-J 2023] 旅游巴士
Problem Description
Xiao Z plans to take a tourist bus to visit a scenic spot he has long wanted to see during the National Day holiday.
The map of the scenic area has a total of locations, with roads connecting them. Location is the entrance of the scenic area, and location is the exit. We define the opening time of the scenic area in a day as time . Starting from time , every units of time, a tourist bus arrives at the entrance, and at the same time a tourist bus departs from the exit.
All roads are one-way only. For each road, walking through it takes exactly unit of time.
Xiao Z wants to take a tourist bus to arrive at the entrance, walk along any path of his choice to reach the exit, and then take a tourist bus to leave. This means that both his arrival time and departure time must be non-negative multiples of . Because there are many tourists during holidays, before the tourist bus leaves the scenic area, Xiao Z only wants to keep moving along the roads, and does not want to wait at any location (including the entrance and exit) or on any road.
Before leaving, Xiao Z suddenly learns that the scenic area limits visitor flow: each road has an “opening time” , and visitors can pass through this road only at times not earlier than .
Please help Xiao Z design a travel plan so that the time when he leaves the scenic area by tourist bus is as early as possible.
Input Format
The first line contains positive integers , representing the number of locations, the number of roads, and the departure interval of the tourist buses.
The next lines each contain non-negative integers , indicating that the -th road starts from location , ends at location , and has an “opening time” of .
Output Format
Output one line containing only one integer, which is the earliest time when Xiao Z can take a tourist bus to leave the scenic area. If there is no feasible travel plan, output -1.
5 5 3
1 2 0
2 5 1
1 3 0
3 4 3
4 5 1
6
Hint
[Sample #1 Explanation]
Xiao Z can arrive at the entrance at time , walk to the exit in the order , and leave at time .
[Sample #2]
See bus/bus2.in and bus/bus2.ans in the attachment.
[Constraints]
For all testdata: , , , , .
| Test Point ID | Special Property | |||
|---|---|---|---|---|
| None | ||||
| None | ||||
| None |
Translated by ChatGPT 5