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 belongning 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
akashmandal21: 2016-06-18 22:36:46

Dijkstra's with priority queue and fast i/o optimization for c++
AC in 0.85 sec :)

pamyjeela: 2016-06-15 13:31:42

finally hallelujah...............

lynx_: 2016-06-03 12:25:48

std::set is bitch ... priority_queue (_/\_)

noob909: 2016-06-02 08:54:40

dijkstra+map+fast i/o

xinnix: 2016-06-01 08:39:12

For source and designation, are they input as same string?

febrianandawp: 2016-05-28 16:16:18

Last edit: 2016-05-28 16:17:04
rishi_devan: 2016-05-12 12:39:39

1) Dijkstra's algorithm with priority queue and map
2) Fast IO optimization for C++

samarsingh: 2016-05-03 19:16:26

Getting NZEC in java code. its working fine on ideone. any suggestions?

Last edit: 2016-05-03 19:17:02
Miguel Angel Ortiz: 2016-03-22 04:15:13

wait, what??? AC? I was expecting TLE on my first try, that felt amazing :D

maciekk1: 2016-02-11 18:25:24

Just remember about empty line after each test case

Last edit: 2016-02-11 18:33:55

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)