#abc204e. E - Rush Hour 2

E - Rush Hour 2

Score : 500500 points

问题描述

在AtCoder共和国,有 NN 座城市和 MM 条道路。

这些城市按编号从 11NN,道路也按编号从 11MM。第 ii 条道路双向连接城市 AiA_i 和城市 BiB_i

该国正经历高峰期,高峰时段在时间 00 点达到顶峰。若你在时间 tt 开始通过第 ii 条道路,则需要 Ci+Dit+1C_i+ \left\lfloor \frac{D_i}{t+1} \right\rfloor 时间单位才能到达另一端。(x\lfloor x\rfloor 表示不超过 xx 的最大整数。)

高桥计划在时间 00 或某个 整数时间 后从城市 11 出发,并前往城市 NN

如果高桥在每个城市停留的 时间单位为整数,请输出他最早能在何时到达城市 NN 的时间。根据本题的约束条件,可以证明答案是一个整数。

如果无法到达城市 NN,则输出 -1

以上为通义千问 qwen-max 翻译,仅供参考。

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 1

2 1
1 2 2 3

Sample Output 1

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 2

2 3
1 2 2 3
1 2 2 1
1 1 1 1

Sample Output 2

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 3

4 2
1 2 3 4
3 4 5 6

Sample Output 3

-1

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

Sample Input 4

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 4

20

update @ 2024/3/10 09:17:43

}