loj#P6853. 「ICPC World Finals 2021」地下城探测者
「ICPC World Finals 2021」地下城探测者
Description
Alice and Bob are in charge of testing a new escape room! In this escape room, customers are trapped in a dungeon and have to explore the entire area. The dungeon consists of rooms connected by exactly corridors. It is possible to travel between any pair of rooms using these corridors.
Two of the dungeon rooms are special. One of these rooms contains a protective idol known as the "helix key." A different room contains a nasty "dome trap," which prevents the player from moving once activated. Entering the room with the trap before acquiring the key will result in the player being trapped in the dungeon forever. The player cannot start in the same room as the key or the trap.
There are different scenarios that Alice and Bob wish to examine. In the scenario, the player starts in room , the key is in room , and the trap is in room . For each scenario, compute the minimum amount of time needed to explore the entire dungeon without getting trapped.
Input
The first line of input contains two integers and , where () is the number of rooms and () is the number of scenarios to consider. Rooms are numbered from to . The next lines each contain three integers , , and indicating that there is a corridor between rooms and () that takes time () to traverse.
Then follow lines: the of these lines contains three distinct integers , , and () indicating the room where the player starts, the room with the key, and the room with the trap, respectively.
Output
For each scenario, output the minimum amount of time needed to visit every room at least once. If it is impossible to visit every room at least once, output impossible
.
5 4
1 2 3
1 3 1
3 4 4
3 5 2
1 2 4
1 4 2
5 2 1
4 3 1
15
17
impossible
12
7 4
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
1 2 3
5 4 1
3 1 4
2 4 5
11
impossible
10
10