102044: [AtCoder]ABC204 E - Rush Hour 2

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $500$ points

Problem Statement

The Republic of AtCoder has $N$ cities and $M$ roads.

The cities are numbered $1$ through $N$, and the roads are numbered $1$ through $M$. Road $i$ connects City $A_i$ and City $B_i$ bidirectionally.

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

Takahashi is planning to depart City $1$ at time $0$ or some integer time later and head to City $N$.

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

Constraints

  • $2 \leq N \leq 10^5$
  • $0 \leq M \leq 10^5$
  • $1 \leq A_i,B_i \leq N$
  • $0 \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:

$N$ $M$
$A_1$ $B_1$ $C_1$ $D_1$
$\vdots$
$A_M$ $B_M$ $C_M$ $D_M$

Output

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


Sample Input 1

2 1
1 2 2 3

Sample Output 1

4

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

It is impossible to reach City $2$ earlier than time $4$.


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 $1$ to City $N$.


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

Input

题意翻译

### 题目大意 给定一张 $n$ 个点,$m$ 条边的无向图,每条边有两个属性 $c_i,d_i$。 你现在位于点 $1$,想要前往点 $n$,现在的时间是 $0$。当时间为 $t$ 时经过第 $i$ 条边所需的时间是 $c_i+\lfloor\frac{d_i}{t+1}\rfloor$。 你可以在城市中停留任意非负整数时间,请求出你到达点 $n$ 所花费的最短时间,如果无法到达,输出 `-1`。

加入题单

上一题 下一题 算法标签: