E - Rush Hour 2 Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

AtCoder国には NN 個の都市と MM 本の道路があります。

都市には 11 から NN の番号が、道路には 11 から MM の番号が振られています。道路 ii は都市 AiA_i と都市 BiB_i を双方向に結びます。

AtCoder国には時刻 00 をピークとするラッシュアワーがあり、時刻 tt に道路 ii の通行を始めると、移動するのに Ci+Dit+1C_i+ \left\lfloor \frac{D_i}{t+1} \right\rfloor の時間がかかります。 ( x\lfloor x\rfloorxx を超えない最大の整数を表す)

高橋君は時刻 00 またはそれ以降の 整数時刻に 都市 11 を出発して、道路を通行することで都市 NN へ向かおうとしています。

高橋君が各都市で 整数時間 待機することができるとき、高橋君が都市 NN に到達することができる最も早い時刻を出力してください。なお、制約の下で答えは整数になることが証明できます。

ただし、都市 NN に到達できないときはかわりに -1 を出力してください。

制約

  • 2N1052 \leq N \leq 10^5
  • 0M1050 \leq M \leq 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 0Ci,Di1090 \leq C_i,D_i \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

NN MM
A1A_1 B1B_1 C1C_1 D1D_1
\vdots
AMA_M BMB_M CMC_M DMD_M

出力

高橋君が都市 NN に到達することができる最も早い時刻を整数で出力せよ。ただし、都市 NN に到達できないときはかわりに -1 を出力せよ。


入力例 1Copy

Copy
2 1
1 2 2 3

出力例 1Copy

Copy
4

最初に都市 11 で時刻 11 まで待機します。そして時刻 11 に道路 11 を使って移動をすると、移動に 2+31+1=32+\left\lfloor \frac{3}{1+1} \right\rfloor = 3 の時間がかかり、都市 22 には時刻 44 に到着することができます。

時刻 44 より早く都市 22 に到着することはできません。


入力例 2Copy

Copy
2 3
1 2 2 3
1 2 2 1
1 1 1 1

出力例 2Copy

Copy
3

同じ都市の組を結ぶ道路が複数ある場合や、同じ都市に戻ってくる道路がある場合もあります。


入力例 3Copy

Copy
4 2
1 2 3 4
3 4 5 6

出力例 3Copy

Copy
-1

都市 11 から都市 NN に至る経路がないこともあります。


入力例 4Copy

Copy
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

Copy
20

Score : 500500 points

Problem Statement

The Republic of AtCoder has NN cities and MM roads.

The cities are numbered 11 through NN, and the roads are numbered 11 through MM. Road ii connects City AiA_i and City BiB_i bidirectionally.

There is a rush hour in the country that peaks at time 00. If you start going through Road ii at time tt, it will take Ci+Dit+1C_i+ \left\lfloor \frac{D_i}{t+1} \right\rfloor time units to reach the other end. (x\lfloor x\rfloor denotes the largest integer not exceeding xx.)

Takahashi is planning to depart City 11 at time 00 or some integer time later and head to City NN.

Print the earliest time when Takahashi can reach City NN 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 NN is unreachable, print -1 instead.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 0M1050 \leq M \leq 10^5
  • 1Ai,BiN1 \leq A_i,B_i \leq N
  • 0Ci,Di1090 \leq C_i,D_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM
A1A_1 B1B_1 C1C_1 D1D_1
\vdots
AMA_M BMB_M CMC_M DMD_M

Output

Print an integer representing the earliest time when Takahashi can reach City NN, or -1 if City NN is unreachable.


Sample Input 1Copy

Copy
2 1
1 2 2 3

Sample Output 1Copy

Copy
4

We will first stay in City 11 until time 11. Then, at time 11, we will start going through Road 11, which will take 2+31+1=32+\left\lfloor \frac{3}{1+1} \right\rfloor = 3 time units before reaching City 22 at time 44.

It is impossible to reach City 22 earlier than time 44.


Sample Input 2Copy

Copy
2 3
1 2 2 3
1 2 2 1
1 1 1 1

Sample Output 2Copy

Copy
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

Copy
4 2
1 2 3 4
3 4 5 6

Sample Output 3Copy

Copy
-1

There may be no path from City 11 to City NN.


Sample Input 4Copy

Copy
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

Copy
20


2025-04-22 (Tue)
09:32:22 +00:00