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
2021-05-13 21:21:16
Simple Dijstra with following note .
Use Directed Graph . Undirected will give you TLE .
2021-01-05 16:36:22
Just in case you need help hooking up with a homo boyfriend, msg me.

Last edit: 2021-01-05 22:19:32
2021-01-05 16:35:42
was getting TLE!!!.
Just break the loop when you have reached the destination vertex.
2020-12-28 13:08:11
1. Why using set gives TLE ? Because it doesn't keep duplicate entries, use multiset else priority queue.
2. What if the destination not connected with source? Assume connected.

Last edit: 2021-01-02 12:50:36
2020-12-25 10:17:30
Was getting TLE
Stopped Dijkstra if the destination is arrived i.e if current node is destination
AC in 0.84
2020-12-09 23:16:17
<snip>

[NG]: Read the footer.

Last edit: 2020-12-10 08:15:34
2020-10-21 10:30:39
AC in one go! Easy problem. Just store these strings into a map and implement standard Dijkstra algorithm.
2020-08-17 18:57:00
Naive Dijkstra->TLE, Dijkstra on sparse graphs->AC. ;)
2020-07-17 14:04:33
if you are getting tle after every possible optimization, check if you have forgetten to clear your adajency list after each test case.
2020-07-10 10:34:03
Use Priority Queue,Set Costed me 6 TLE

Last edit: 2020-07-10 10:34:21
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.