#P14610. [NWRRC 2025] Keys and Grates

[NWRRC 2025] Keys and Grates

题目描述

Katniss is in a very long straight tunnel and wants to get out of it. She can move along the tunnel in both directions. There is exactly one hatch in the tunnel, and if she reaches it, she can exit.

Unfortunately, it's not that simple. There are grates blocking the entire width of the tunnel at nn locations. The grates are locked, and Katniss cannot pass through a grate until she unlocks it. Fortunately, there are nn keys located at nn points along the tunnel. Each grate can be unlocked by exactly one of these nn keys. Once a grate is unlocked, Katniss can freely and repeatedly pass through it. She can pick up and hold an unlimited number of keys.

Katniss knows her own location, as well as the locations of all nn keys, all nn grates, and the hatch. She also knows which key unlocks which grate. Determine the minimum distance she must travel to escape.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1t51041 \le t \le 5 \cdot 10^4). The description of the test cases follows.

The first line of each test case contains three integers nn, ss, and hh, denoting the number of grates, the~initial coordinate of Katniss, and the coordinate of the hatch (1n21051 \le n \le 2 \cdot 10^5; 106s,h106-10^6 \le s, h \le 10^6).

The ii-th of the following nn lines contains two integers kik_i and gig_i, denoting the coordinate of the ii-th key and the coordinate of the grate that can be unlocked with this key (106ki,gi106-10^6 \le k_i, g_i \le 10^6).

All 2n+22n+2 integers ss, hh, kik_i, and i\ell_i are distinct.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

输出格式

For each test case, print the minimum possible length of Katniss' escape route. If it is impossible to escape, print 1-1 instead.

3
5 5 13
3 7
8 14
-1 11
9 1
2 10
4 40 0
20 80
50 30
70 60
90 10
1 -1 6
11 8
32
-1
7

提示

In the first test case, one of the shortest escape routes goes as follows: 55 (initial) \rightarrow 33 (pick up key 11) \rightarrow 77 (unlock grate 11) \rightarrow 99 (pick up key 44) \rightarrow 11 (unlock grate 44) \rightarrow 1-1 (pick up key 33) \rightarrow 22 (pick up key 55) \rightarrow 1010 (unlock grate 55) \rightarrow 1111 (unlock grate 33) \rightarrow 1313 (hatch).