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:256MB
Cluster: Pyramid (Intel Pentium III 733 MHz)
Languages:All except: NODEJS PERL 6
Resource: DASM Programming League 2003 (problemset 11)

hide comments
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 ??
2014-06-30 19:32:06 Ashwini
Phew... after a lot of hard work ac finally @4.88 sec(just made it). Learnt a new stl priority queue though. min heap was getting tled. This was one hell of an optimisation problem. First i would suggest everybody to get aced here
http://www.spoj.com/problems/TSHPATH/
This is tutorial version of same problem where time has been relaxed a lot...
2014-06-26 11:03:56 sobriquet
AC at 4.99 sec :-). Use fast I/O if using maps and vectors and string in C++.
2014-06-13 06:17:12 Caio Hoffman
My solution constantly gets "time limit exceeded".
What may be happening?
I've already submitted it to another contest site and the submission was accepted in the very first time. It took 60 ms and no more than 3 MB.
=(

Last edit: 2014-06-13 06:18:26
2014-06-06 05:37:20 B Soma Naik
can some one check my code: http://ideone.com/jSUhJb
getting so many WA./
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.