#P11102. [ROI 2022] 搬货 (Day 2)
[ROI 2022] 搬货 (Day 2)
Background
Translated from ROI 2022 D2T3.
Problem Description
A warehouse operator needs to use a special forklift to move a heavy box. The warehouse can be simplified as rooms connected by corridors. You can travel from any room to any other room using the corridors. The rooms are numbered from to . Corridor directly connects rooms and , and it can be used in both directions.
The forklift can lift and lower the cargo. If it is not carrying the cargo, it can move between rooms and corridors. Initially, the forklift is in room and is carrying the cargo in the lifted state. The forklift can perform the following actions:
- If the forklift is in room and is carrying the lifted cargo, it can place the cargo into room , provided that rooms and are directly connected. After this action, the forklift is no longer carrying the cargo and can move.
- If the forklift is in room , the cargo is placed in room , and rooms and are directly connected, then the forklift can lift the cargo without moving. After this action, the forklift remains in room and carries the lifted cargo, and it cannot move until it places the cargo down.
- If the forklift is not carrying the cargo, it can move through corridors between rooms, but it cannot pass through a room that contains the cargo.
Moving an empty forklift between rooms is very fast, much faster than lifting or lowering the cargo. Therefore, we assume that performing action 1 or action 2 takes one unit of time, while action 3 is instantaneous. For each room , determine the shortest time needed for the forklift to go from the initial state (in room and carrying the lifted cargo) to room and still be carrying the lifted cargo, or determine that this is impossible.
Input Format
Each input contains multiple test cases. The first line contains an integer (), the number of test cases. The description of each test case follows.
The first line of each test case contains two integers and (), the numbers of rooms and corridors in the warehouse.
The next lines each contain two integers and (), meaning that the -th corridor connects these two rooms. It is guaranteed that each unordered pair of rooms connected by a corridor appears at most once (i.e., each corridor is given only once). It is guaranteed that if all rooms are empty, then you can reach any room from any other room using the corridors, i.e., the graph is connected.
Let be the sum of over all test cases, and be the sum of over all test cases. It is guaranteed that and .
Output Format
For each test case, output numbers. The -th number should be the minimum number of lift/lower operations (i.e., the shortest time) needed for the forklift to start from room and reach room (while still carrying the lifted cargo). If it is impossible, the -th number should be .
4
4 4
1 2
2 3
3 4
4 1
5 5
1 2
2 3
3 4
4 5
5 1
5 6
1 2
3 2
1 3
3 5
5 4
3 4
9 12
1 2
2 3
3 1
4 5
5 6
6 4
7 8
8 9
9 7
3 6
6 9
9 3
-1 2 -1
4 2 2 4
2 2 4 4
2 2 6 6 4 6 6 4
Hint
Explanation for part of the samples:
In the fourth test case, to make the forklift go from room (carrying the lifted cargo) to room as fast as possible (and still carry the lifted cargo), it can perform the following actions:
- Place the cargo into room . This takes one unit of time.
- Move to room . This takes no time.
- Lift the cargo from room . This takes one unit of time.
- Place the cargo into room . This takes one unit of time.
- Move to room . This takes no time.
- Lift the cargo from room . This takes one unit of time.
- Place the cargo into room . This takes one unit of time.
- Move to room . This takes no time.
- Lift the cargo from room . This takes one unit of time.
In total, it takes units of time.
| Subtask | Score | Special Properties | ||
|---|---|---|---|---|
| For any , there is a corridor connecting rooms and , and there is also a corridor connecting rooms and | ||||
| Each room has at most corridors | ||||
Translated by ChatGPT 5