Sphere Online Judge

SPOJ Problem Set (classical)

15. The Shortest Path

Problem code: SHPATH


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
Added by:Darek Dereniowski
Date:2004-05-10
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel Pentium G860 3GHz)
Languages:All except: NODEJS PERL 6
Resource: DASM Programming League 2003 (problemset 11)

hide comments
2014-11-14 16:11:09 Yokhan Dukhin
Finally 0.5 sec, Dijkstra + priority_queue + fast IO

Last edit: 2014-11-14 16:11:26
2014-11-13 17:54:15 Yokhan Dukhin
have tle, can anyone provide a huge test case.
2014-10-24 14:25:35 Nilesh Sah
AC in 1.60s with Dijkstra + set and fast I/O.
2014-10-06 20:21:23 gaurav gupta
Which algo is used to solve in 0.00s?

Last edit: 2014-10-06 20:21:44
2014-09-29 15:51:28 Vidur Katyal
Had to optimize a lot to pass the time limit. Very nice problem :D
Use fast I/O
2014-09-17 06:05:20 Umair Khan
Dijkstra + min heap = AC in 4.20s

FTW!
2014-09-02 10:53:35 Amitayush Thakur
Optimized from 17.88s in TSHPATH to 3.87s :) . Nice question.
2014-08-09 20:16:30 tapan sahni
dont use heaps it will give tle
2014-07-23 09:14:47 Bony Roopchandani
Finally after 24 combined attempts from TSHPATH as well, got AC at 4.90s. :D
2014-07-19 14:33:45 yousef hadder
Did any one mange to solve it with SPFA ??
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.