SHPATH - The Shortest Path

You are given a list of cities. Each direct connection between two cities has its transportation cost (an integer bigger than 0). The goal is to find the paths of minimum cost between pairs of cities. Assume that the cost of each path (which is the sum of costs of all direct connections belonging to this path) is at most 200000. The name of a city is a string containing characters a ... z and is at most 10 characters long.

Input

s [the number of tests ≤ 10]
n [the number of cities ≤ 10000]
NAME [city name]
p [the number of neighbours of city NAME]
nr cost [nr - index of a city connected to NAME (the index of the first city is 1)]
        [cost - the transportation cost]
r [the number of paths to find ≤ 100]
NAME1 NAME2 [NAME1 - source, NAME2 - destination]
[empty line separating the tests]

Output

cost [the minimum transportation cost from city NAME1 to city NAME2 (one per line)]

Example

Input:
1
4
gdansk
2
2 1
3 3
bydgoszcz
3
1 1
3 1
4 4
torun
3
1 3
2 1
4 1
warszawa
2
2 4
3 1
2
gdansk warszawa
bydgoszcz warszawa

Output:
3
2

Warning: large Input/Output data, be careful with certain languages


Added by:Darek Dereniowski
Date:2004-05-10
Time limit:1.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:DASM Programming League 2003 (problemset 11)

hide comments
2017-04-01 20:19:04
AC in one go using priority queue
2017-03-29 10:40:28
AC in one go without using Fast I/O but it took 0.37 sec
2017-03-13 18:51:34
if you are getting tle :
1. pq over sets.
2. maps can be your friend.
3. fast i/o can be really helfull too...
2017-03-11 10:14:29
FAST I/O, map and dijkstra's algo,
AC in 2nd GO!
2017-03-09 22:03:19
priority queue _/\_
2017-02-11 23:01:15 hacker_sk
mine 0.32 sec in cpp hahaha :D
2016-11-13 11:57:54
getting NZEC in SPOJ while running fine in ideone
Any suggestions?
2016-09-14 17:40:42
yo..!!0.41 seconds
2016-08-15 10:29:01
AC in 0.46 sec
Learned a lot
2016-08-02 17:32:46
The logic of the question is simple however its a very nice question because of the optimizations required to pass in the given Time Limit.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.