#P3645. [APIO2015] 雅加达的摩天楼

    ID: 1437 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2015APIO枚举,暴力最短路块状链表,块状数组,分块

[APIO2015] 雅加达的摩天楼

Description

There are NN skyscrapers in Jakarta, the capital of Indonesia, arranged in a straight line and numbered from 00 to N1N - 1 from left to right. There are no other skyscrapers in Jakarta besides these NN.

There are MM mysterious creatures called "doge" living in Jakarta, numbered from 00 to M1M - 1. Doge ii initially lives at skyscraper BiB_i. Each doge has a mysterious power that allows it to jump between skyscrapers. The jumping ability of doge ii is PiP_i (Pi>0P_i > 0).

In one jump, a doge at skyscraper bb with jump ability pp can jump to skyscraper bpb - p (if 0bp<N0 \leq b - p < N) or to skyscraper b+pb + p (if 0b+p<N0 \leq b + p < N).

Doge 00 is the leader of all doges, and it needs to deliver an urgent message to doge 11 as soon as possible. Any doge that has received the message has two options:

  • Jump to another skyscraper.
  • Pass the message to another doge currently on the same skyscraper.

Please help the doges compute the minimum total number of jumps required to deliver the message from doge 00 to doge 11, or determine that the message can never reach doge 11.

Input Format

The first line contains two integers NN and MM.

Each of the next MM lines contains two integers BiB_i and PiP_i.

Output Format

Output one line with the minimum number of steps required. If the message can never reach doge 11, output 1-1.

5 3
0 2
1 1
4 1
5

Hint

Sample explanation:

Below is a solution with a total of 55 jumps:

  • Doge 00 jumps to skyscraper 22, then jumps to skyscraper 44 (22 jumps).
  • Doge 00 passes the message to doge 22.
  • Doge 22 jumps to skyscraper 33, then jumps to skyscraper 22, and then jumps to skyscraper 11 (33 jumps).
  • Doge 22 passes the message to doge 11.

Constraints:

All testdata guarantee 0Bi<N0 \leq B_i < N.

Subtask 1 (10 points)

  • 1N101 \leq N \leq 10
  • 1Pi101 \leq P_i \leq 10
  • 2M32 \leq M \leq 3

Subtask 2 (12 points)

  • 1N1001 \leq N \leq 100
  • 1Pi1001 \leq P_i \leq 100
  • 2M20002 \leq M \leq 2000

Subtask 3 (14 points)

  • 1N20001 \leq N \leq 2000
  • 1Pi20001 \leq P_i \leq 2000
  • 2M20002 \leq M \leq 2000

Subtask 4 (21 points)

  • 1N20001 \leq N \leq 2000
  • 1Pi20001 \leq P_i \leq 2000
  • 2M300002 \leq M \leq 30000

Subtask 5 (43 points)

  • 1N300001 \leq N \leq 30000
  • 1Pi300001 \leq P_i \leq 30000
  • 2M300002 \leq M \leq 30000

Translated by ChatGPT 5