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.


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]


cost [the minimum transportation cost from city NAME1 to city NAME2 (one per line)]


2 1
3 3
1 1
3 1
4 4
1 3
2 1
4 1
2 4
3 1
gdansk warszawa
bydgoszcz warszawa


Warning: large Input/Output data, be careful with certain languages
Added by:Darek Dereniowski
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-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

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
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++.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.