#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 locations. The grates are locked, and Katniss cannot pass through a grate until she unlocks it. Fortunately, there are keys located at points along the tunnel. Each grate can be unlocked by exactly one of these 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 keys, all 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 (). The description of the test cases follows.
The first line of each test case contains three integers , , and , denoting the number of grates, the~initial coordinate of Katniss, and the coordinate of the hatch (; ).
The -th of the following lines contains two integers and , denoting the coordinate of the -th key and the coordinate of the grate that can be unlocked with this key ().
All integers , , , and are distinct.
It is guaranteed that the sum of over all test cases does not exceed .
输出格式
For each test case, print the minimum possible length of Katniss' escape route. If it is impossible to escape, print 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: (initial) (pick up key ) (unlock grate ) (pick up key ) (unlock grate ) (pick up key ) (pick up key ) (unlock grate ) (unlock grate ) (hatch).
京公网安备 11011102002149号