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
2016-03-22 04:15:13 Miguel Angel Ortiz
wait, what??? AC? I was expecting TLE on my first try, that felt amazing :D
2016-02-11 18:25:24
Just remember about empty line after each test case

Last edit: 2016-02-11 18:33:55
2016-01-30 12:07:14 Prateek Agarwal
Finally AC after a lot of WA's!! No C++ STL used(except for city and index mapping). Used min heap operations.
2016-01-24 15:00:38 epsilon
wow stl c++ :)
0.64sec

Last edit: 2016-01-24 15:00:59
2016-01-22 05:38:38 satyam bajpai
is it a directed graph ?
2015-12-19 21:07:29 Divyansh Shukla
1.39s :) using Java. Hints: use adjacency list for storage and inbuilt priority queue for Dijkstra.
2015-11-19 15:19:11
Used unordered_map<string,int>. compile error for g++4.3.2 and g++ 5.1. Accepted for C14 g++5.1. Any idea?
2015-11-13 12:24:52
replaced cin by scanf and got ac........ :)
2015-09-28 20:07:31 ARUN P
Are the cities given in order according to their indices?
2015-09-04 22:11:33 Rohit Agarwal
Wow. Really nice question. It teaches the importance of optimization.
Finally AC after 3 TLE's.
Optimizations I did:-
1. Fast I/O: use getchar_unlocked for all the input
2. Use Djikstra with min heap.
3. Avoid using a graph like this graph[MAX][MAX].. becasue this wil create the graph which will take order of 10power8. You need something around O(10pow7) for AC. Go for edgelist representation.

To check your Time Limit.. solve TSHPATH. the time limit is ~ 5secs.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.