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
2023-12-02 06:59:50
Anyone wondering, use Djikstra's for each path. Gives TLE for Flloyd Warshall;
2023-08-26 16:18:49
Note to self
1. Use directed graph
2. Instead of calculating for all the cities and then answering after that , calculate for each query just like simple bfs.
2023-03-29 20:59:52
using simple priority queue with multiple query will help and directed with be accepted and undirected also ok there are no critical test cases if you are using simple set or simple queue or simple algorithm(nor set neither queue) then it will give tle
2023-01-11 11:14:00
Polska gurom !!!!!
2022-12-27 07:27:25
Note:
1) clear adjacency list at each test case.
2) assuming the graph as directed, solution gets accepted. So no need to make undirected graph.
3) TSHPATH can be solved using set only, but for SHPATH use priority queue.
4) In Dijkstra implementation, if end node is processed, we can end the search. Without ending the search, solution gets accepted too.

Last edit: 2022-12-28 11:41:23
2022-12-16 10:54:55
anyone give me a testcase..
2021-12-21 18:53:47
Is python code possible for this problem ?

[NG]: Probably not. Try TSHPATH and submit as PyPy. My best time is 1.51s.

Last edit: 2021-12-22 02:31:20
2021-08-13 08:32:56
Is the graph directed or undirected? why both of the solution get accepted?
2021-08-01 21:51:28
just use priority queue to avoid TLE , it is faster than using set in dijkstra
2021-06-26 12:44:01 Waseem Ahmed
Finally got it accepted.

Need to read the comments from the users carefully if you get a TLE. Simple Dijkstra but data structures used tend to dominate the acceptance of the solution. Adjacency matrix stored as a standard 2x2 matrix will get you a TLE.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.