#P16472. [GKS 2013 #A] Spaceship Defence
[GKS 2013 #A] Spaceship Defence
题目描述
The enemy has invaded your spaceship, and only superior tactics will allow you to defend it! To travel around your spaceship, your soldiers will use two devices: teleporters and turbolifts.
Teleporters allow your soldiers to move instantly between rooms. Every room contains a teleporter, and rooms are color-coded: if a soldier is in a room with some color, she can use the teleporter in that room to immediately move to any other room with the same color.
Turbolifts allow your soldiers to move between rooms more slowly. A turbolift is like an elevator that moves in many directions. Each turbolift moves from one room to one other room, and it takes a certain amount of time to travel. Notes about turbolifts:
- Turbolifts are not two-way: if a turbolift moves soldiers from room to room , the same turbolift cannot move soldiers from room to room , although there might be another turbolift that does that.
- More than one soldier can use the same turbolift, and they do not interfere with each other in any way.
You will be given the locations and destinations of several soldiers. For each soldier, output the minimum amount of time it could take that soldier to travel from his location to his destination.
输入格式
The first line of the input gives the number of test cases, . test cases follow.
For every test case:
The first line of every test case contains an integer , which is the number of rooms in your spaceship. The rooms are numbered from to . The following lines each contain a string telling the color of the rooms, from room to room . The strings only contain characters a–z (the lower-case English letters) and – (the number to ), and the length of each string will be less than or equal to .
The next line in the test case is an integer , which indicates the number of turbolifts in your spaceship. The following lines each contain space-separated integers , , , telling us that there is a turbolift that can transport soldiers from room to room in seconds.
The next line in the test case contains an integer , which is the number of soldiers at your command. The following lines each contain two integers: the location and destination of one soldier, and .
输出格式
For each test case, output one line containing only the string "Case #x:", where is the number of the test case (starting from ). On the next lines, output a single integer: on line , the smallest number of seconds it could take for a soldier to travel from to . If there is no path from to , the integer you output should be .
3
3
gl
t3
t3
3
1 2 217
3 2 567
1 1 21
2
2 1
2 3
4
ca
bl
bl
8z
0
3
1 2
2 3
1 1
8
re
b7
ye
gr
0l
0l
ye
b7
7
4 1 19
2 4 21
2 5 317
4 5 34
4 7 3
4 8 265
8 6 71
3
4 3
2 6
1 4
Case #1:
-1
0
Case #2:
-1
0
0
Case #3:
3
55
-1
提示
Limits
.
.
.
.
Test set 1 - Visible
.
.
.
Test set 2 - Hidden
.
.
.