Heavy Cargo
Time limit2sMemory limit1024 MB
For each room p from 2 to n, find the fewest lifts and put-downs needed to carry the box from room 1 to room p, or report -1.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Statement
Warehouse operators need to move a heavy box with a special forklift. The warehouse can be modeled as rooms connected by corridors. From any room, you can reach any other room by walking through corridors. The rooms are numbered from 1 to . Corridor directly connects rooms and , and it can be traversed in both directions.
The forklift can lift and put down the box. When it is not holding the box, it can move through free rooms and corridors. At the start, the forklift is in room 1 and holds the box. The forklift supports these actions:
- If the forklift is in room and holds the box, it can put the box down in room without moving, provided rooms and are directly connected by a corridor. After this action, the forklift no longer holds the box and can move.
- If the forklift is in room and the box is in room , it can pick up the box without moving, provided rooms and are directly connected by a corridor. After this action, the forklift stays in room and holds the box. It cannot move until it puts the box down.
- If the forklift is not holding the box, it can move through corridors and rooms. It cannot pass through a room where the box lies.
An empty forklift moves much faster than it lifts or lowers the box. So action 1 or action 2 takes one unit of time, and action 3 takes no time. For each room (), find the minimum time the forklift needs to go from the start state (room 1, holding the box) to room while holding the box. If that is impossible, report it.
Input
Each test consists of several input sets. The first line contains one integer (), the number of input sets. Then the input sets follow.
The first line of each input set contains two integers and (, ), the number of rooms and corridors.
The next lines each contain two integers and (, ), the numbers of the rooms connected by the -th corridor. Each pair of rooms connected by a corridor is listed exactly once. It is guaranteed that if all rooms are empty, you can reach any room from any other room by walking through corridors.
Let the sum of over all input sets be at most 500000, and the sum of be at most 500000.
Output
For each input set, print numbers. The -th number is the minimum number of lifts and put-downs of the box needed for the forklift to reach room while holding the box. If it is impossible, the -th number is .
Hint
In the fourth input set, the forklift can reach room 4 holding the box in the fastest way as follows:
- Put the box down in room 2. This takes one unit of time.
- Move to room 3. This takes no time.
- Pick up the box from room 2. This takes one unit of time.
- Put the box down in room 9. This takes one unit of time.
- Move to room 6. This takes no time.
- Pick up the box from room 9. This takes one unit of time.
- Put the box down in room 5. This takes one unit of time.
- Move to room 4. This takes no time.
- Pick up the box from room 5. This takes one unit of time.
In total, 6 units of time are spent.