#P10433. [JOIST 2024] 棋盘游戏 / Board Game
[JOIST 2024] 棋盘游戏 / Board Game
Problem Description
There is a board game played by players. The board consists of cells numbered from to and paths numbered from to . Path () connects cell and cell in both directions.
There are two types of cells on the board: reactivating cells and stopping cells.
This information is given by a string of length . consists of and . The -th character of () is '0' if cell is a reactivating cell, and '1' if cell is a stopping cell.
This board game is played by players numbered from to . Each player has their own piece, and the game starts with each player placing their piece on a specific cell. Initially, player () places their piece on cell . Note that multiple players' pieces may be on the same cell.
The game proceeds with the players taking turns, starting from player and continuing in numerical order. After player finishes their turn, player starts their turn (if , then player starts their turn). On their turn, each player performs the following:
- Choose a cell connected to the cell where their piece currently is by a path, and move their piece to the chosen cell.
- If the destination cell is a reactivating cell, repeat step 1 and continue their turn. If the destination cell is a stopping cell, end their turn.
A team of members representing Japan in this board game, including JOI-kun, is studying cooperative strategies to conquer this game quickly. They are currently studying the following question:
What is the minimum total number of moves needed by the players to place player 's piece on cell ? Even if player 's piece is placed on cell in the middle of a turn, it is considered successful.
Given the information about the game board and the initial positions of each player's piece, write a program to compute the answer to the above question for each .
Input Format
Read the following data from standard input:
- ...
Output Format
Output lines. On line (), output the minimum total number of moves required for the players to place player 's piece on cell .
5 5 2
1 2
2 3
2 4
3 5
4 5
00000
1 5
0
1
2
2
3
5 5 2
1 2
2 3
2 4
3 5
4 5
01000
1 5
0
1
4
4
5
5 5 2
1 2
2 3
2 4
3 5
4 5
01100
1 5
0
1
3
3
4
8 7 5
1 3
5 7
4 6
2 6
2 3
7 8
1 5
10011010
4 6 4 7 1
4
2
3
0
10
1
17
24
12 13 3
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
1 10
2 9
7 12
11 12
110000011101
1 9 11
0
1
4
5
6
7
8
8
4
1
13
9
Hint
Sample Explanation 1
Since player 's piece starts from cell , the answer for is .
For , in the first move, player can move their piece from cell to cell . Therefore, the answer for is .
For , they can place player 's piece on cell in the following moves:
- In the first move, player moves their piece from cell to cell . Since cell is a reactivating cell, player 's turn continues.
- In the second move, player moves their piece from cell to cell .
Since they cannot place player 's piece on cell in move or less, the answer for is .
Similarly, it can be verified that the answer for is , and the answer for is .
This sample input satisfies the constraints of subtasks .
Sample Explanation 2
For , they can place player 's piece on cell in the following moves:
- In the first move, player moves their piece from cell to cell . Since cell is a stopping cell, it is then player 's turn.
- In the second move, player moves their piece from cell to cell . Since cell is a reactivating cell, player 's turn continues.
- In the third move, player moves their piece from cell to cell . Since cell is a stopping cell, it is then player 's turn.
- In the fourth move, player moves their piece from cell to cell .
Since they cannot place player 's piece on cell in moves or less, the answer for is .
This sample input satisfies the constraints of subtasks .
Sample Explanation 3
This sample input satisfies the constraints of subtasks .
Sample Explanation 4
This sample input satisfies the constraints of subtasks .
Sample Explanation 5
This sample input satisfies the constraints of subtasks .
Constraints
- ()
- , () are distinct.
- It is possible to reach any cell from any other cell by traversing one or more paths.
- is a string of length $N consisting of '0' and '1'.
- ()
- , , and are integers.
- and are integers ().
- is an integer ().
Subtasks
- (3 points) There are no stopping cells.
- (7 points) There is exactly one stopping cell.
- (7 points) There are exactly two stopping cells.
- (19 points) , ,
- (23 points)
- (9 points)
- (23 points) , ,
- (9 points) No additional constraints.
Translated by ChatGPT 5