1829: [CSP-J 2023] 旅游巴士

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:6 解决:2

题目描述

小 Z 打算在国庆假期期间搭乘旅游巴士去一处他向往已久的景点旅游。

旅游景点的地图共有 lns="http://www.w3.org/1998/Math/MathML"> 处地点,在这些地点之间连有 lns="http://www.w3.org/1998/Math/MathML"> 条道路。其中 lns="http://www.w3.org/1998/Math/MathML">1 号地点为景区入口,lns="http://www.w3.org/1998/Math/MathML"> 号地点为景区出口。我们把一天当中景区开门营业的时间记为 lns="http://www.w3.org/1998/Math/MathML">0 时刻,则从 lns="http://www.w3.org/1998/Math/MathML">0 时刻起,每间隔 lns="http://www.w3.org/1998/Math/MathML"> 单位时间便有一辆旅游巴士到达景区入口,同时有一辆旅游巴士从景区出口驶离景区。

所有道路均只能单向通行。对于每条道路,游客步行通过的用时均为恰好 lns="http://www.w3.org/1998/Math/MathML">1 单位时间。

小 Z 希望乘坐旅游巴士到达景区入口,并沿着自己选择的任意路径走到景区出口,再乘坐旅游巴士离开,这意味着他到达和离开景区的时间都必须是 lns="http://www.w3.org/1998/Math/MathML"> 的非负整数倍。由于节假日客流众多,小 Z 在旅游巴士离开景区前只想一直沿着景区道路移动,而不想在任何地点(包括景区入口和出口)或者道路上停留

出发前,小 Z 忽然得知:景区采取了限制客流的方法,对于每条道路均设置了一个 “开放时间”lns="http://www.w3.org/1998/Math/MathML">,游客只有不早于 lns="http://www.w3.org/1998/Math/MathML"> 时刻才能通过这条道路。

请帮助小 Z 设计一个旅游方案,使得他乘坐旅游巴士离开景区的时间尽量地早。

输入

输入的第一行包含 3 个正整数 lns="http://www.w3.org/1998/Math/MathML">,,,表示旅游景点的地点数、道路数,以及旅游巴士的发车间隔。

输入的接下来 lns="http://www.w3.org/1998/Math/MathML"> 行,每行包含 3 个非负整数 lns="http://www.w3.org/1998/Math/MathML">,,,表示第 lns="http://www.w3.org/1998/Math/MathML"> 条道路从地点 lns="http://www.w3.org/1998/Math/MathML"> 出发,到达地点 lns="http://www.w3.org/1998/Math/MathML">,道路的“开放时间”为 lns="http://www.w3.org/1998/Math/MathML">

输出

输出一行,仅包含一个整数,表示小 Z 最早乘坐旅游巴士离开景区的时刻。如果不存在符合要求的旅游方案,输出 -1

样例输入 复制

5 5 3
1 2 0
2 5 1
1 3 0
3 4 3
4 5 1

样例输出 复制

6

提示

【样例 #1 解释】

小 Z 可以在 lns="http://www.w3.org/1998/Math/MathML">3 时刻到达景区入口,沿 lns="http://www.w3.org/1998/Math/MathML">1345 的顺序走到景区出口,并在 lns="http://www.w3.org/1998/Math/MathML">6 时刻离开。

【样例 #2】

见附件中的 bus/bus2.in 与 bus/bus2.ans


【数据范围】

对于所有测试数据有:210412×10411001,0106



附件下载

bus.zip