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


hide comments
bani_raheja: 2017-10-28 22:07:59

ac in one go!
Just take care that you don't traverse the whole graph. As soon as the shortest distance of the key node is satisfied, break out of the Dijkstra algorithm and print the result

kmkhan_014: 2017-10-25 19:41:35

The edges are all directed. if assumed undirected the judge may throw TLE

tusharuppal: 2017-10-02 14:48:01

AC in one go..
use map
fast I/O..

parthendo: 2017-09-19 16:46:52

AC in one go :)

KD : 2017-08-31 20:01:27

easy one :P

luka13: 2017-05-31 23:42:05

what we must output if we can't reach the city?

babur: 2017-05-25 05:32:22

Remember To clear the vector

epsilonalpha: 2017-04-18 15:33:18

A/C after 5 TLE.
For the first time, STL's list failed me over STL's vector for my adjacency list representation.
1. Use vector over list.
2. unordered_map
3. priority_queue
4. getchar_unlocked fast IO

anurag_tangri: 2017-04-18 11:06:01

fast i/o _/\_

ANKIT JAIN: 2017-04-17 20:52:31

AC in one go :) Happy !!!


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)