

Time Limit: 2 sec / Memory Limit: 1024 MB
配点 : 点
問題文
AtCoder国には 個の都市と 本の道路があります。
都市には から の番号が、道路には から の番号が振られています。道路 は都市 と都市 を双方向に結びます。
AtCoder国には時刻 をピークとするラッシュアワーがあり、時刻 に道路 の通行を始めると、移動するのに の時間がかかります。 ( は を超えない最大の整数を表す)
高橋君は時刻 またはそれ以降の 整数時刻に 都市 を出発して、道路を通行することで都市 へ向かおうとしています。
高橋君が各都市で 整数時間 待機することができるとき、高橋君が都市 に到達することができる最も早い時刻を出力してください。なお、制約の下で答えは整数になることが証明できます。
ただし、都市 に到達できないときはかわりに -1
を出力してください。
制約
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
出力
高橋君が都市 に到達することができる最も早い時刻を整数で出力せよ。ただし、都市 に到達できないときはかわりに -1
を出力せよ。
入力例 1Copy
2 1 1 2 2 3
出力例 1Copy
4
最初に都市 で時刻 まで待機します。そして時刻 に道路 を使って移動をすると、移動に の時間がかかり、都市 には時刻 に到着することができます。
時刻 より早く都市 に到着することはできません。
入力例 2Copy
2 3 1 2 2 3 1 2 2 1 1 1 1 1
出力例 2Copy
3
同じ都市の組を結ぶ道路が複数ある場合や、同じ都市に戻ってくる道路がある場合もあります。
入力例 3Copy
4 2 1 2 3 4 3 4 5 6
出力例 3Copy
-1
都市 から都市 に至る経路がないこともあります。
入力例 4Copy
6 9 1 1 0 0 1 3 1 2 1 5 2 3 5 2 16 5 2 6 1 10 3 4 3 4 3 5 3 10 5 6 1 100 4 2 0 110
出力例 4Copy
20
Score : points
Problem Statement
The Republic of AtCoder has cities and roads.
The cities are numbered through , and the roads are numbered through . Road connects City and City bidirectionally.
There is a rush hour in the country that peaks at time . If you start going through Road at time , it will take time units to reach the other end. ( denotes the largest integer not exceeding .)
Takahashi is planning to depart City at time or some integer time later and head to City .
Print the earliest time when Takahashi can reach City if he can stay in each city for an integer number of time units. It can be proved that the answer is an integer under the Constraints of this problem.
If City is unreachable, print -1
instead.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print an integer representing the earliest time when Takahashi can reach City , or -1
if City is unreachable.
Sample Input 1Copy
2 1 1 2 2 3
Sample Output 1Copy
4
We will first stay in City until time . Then, at time , we will start going through Road , which will take time units before reaching City at time .
It is impossible to reach City earlier than time .
Sample Input 2Copy
2 3 1 2 2 3 1 2 2 1 1 1 1 1
Sample Output 2Copy
3
There may be multiple roads connecting the same pair of cities, and a road going from a city to the same city.
Sample Input 3Copy
4 2 1 2 3 4 3 4 5 6
Sample Output 3Copy
-1
There may be no path from City to City .
Sample Input 4Copy
6 9 1 1 0 0 1 3 1 2 1 5 2 3 5 2 16 5 2 6 1 10 3 4 3 4 3 5 3 10 5 6 1 100 4 2 0 110
Sample Output 4Copy
20