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.



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

hide comments
sangmai: 2019-12-31 12:49:16

After I switch to directed graph I got AC with 0.57 sec.
Undirected graph gives 1.4 sec :(

Last edit: 2019-12-31 12:49:30
sangmai: 2019-12-30 15:08:39

Why are people using directed graph? The problem statement does not say that. Does it affect the correctness of the program?

abhishek_chn: 2019-10-23 19:47:05

AC in 0.39 secs :P
Did in C lang

ankitsinha3005: 2019-08-23 19:44:41

Was Getting a TLE just because i was using map ,Dont forget to use unordered_map

importme: 2019-07-20 17:14:18

can we consider that the cities asked are connected

shahayush457: 2019-06-28 15:18:14

Just implement naive dijkstra without any optimization !!!

eshanrh: 2019-06-17 09:16:12


Floyd-Warshall's doesn't work with these bounds.
There are at most 10^4 cities, and with floyd's you get 10^4^3 loops = 10^12 loops. Anything over 10^8 will probably not run in time. The rule of thumb I use is 10^8 = 1 second, 10^9 = 10 seconds and so on.

hitesh87: 2019-05-17 19:43:35

@harshraj22aug Didn't try it but it might certainly increase TC. e.g if no. of query are small like 1,2 etc then Floyd Warshall Algorithm will take O(V^3) while Djikstra can handle in O(V^2).
Don't relay on spoj ool kit for this.

harshraj22aug: 2019-05-17 12:58:53

Recently I came to know about Floyd Warshall Algorithm, which gives the shortest path between all pairs of points in the graph. I wonder why isn't anyone using that here, rather than doing Dijkstra for every query ?
(Is it due to huge difference in time complexity in both algorithms , or am I failing to understand something important ? )

Narendra yadav: 2019-04-19 22:14:44

How can you .... "Stop Dijkstra if the destination is arrived i.e if current node is destination". Won't this give WA (non-optimal) ?

Last edit: 2019-04-19 22:15:00

Added by:Darek Dereniowski
Time limit:1.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource:DASM Programming League 2003 (problemset 11)