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
mayurcoder: 2018-06-06 08:17:48

Don' t use Java. You will get tle in this question no matter what optimization you do.

sikh_coder: 2018-05-28 20:23:32

very imp note: make sure to clear your graph after each test case, will save you a wa :)

techobist: 2018-05-28 14:33:19

Directed graph will save you from TLE :)

terminator222: 2018-05-25 12:49:29

multiset rocks!

kygas: 2018-04-01 23:08:47

No, no, I am supposed to teach kids the Dijkstra, and I am using this problem as an example.
But I figured that a solution I wanted to use was gone... I am a kid myself and story on how I got to explain Dijkstra is long and not important, but I am not going to use that code in competition It's just for explaining Dijkstra or better to show how this problem is solved, no unfair advantage will be gained with that code, don't worry. :)
C++ is the language I need...

Last edit: 2018-04-01 23:12:19
nadstratosfer: 2018-04-01 04:03:58

A state competition in ripping off someone else's code? By what criteria a winner is determined there, kygas?

kygas: 2018-03-31 20:59:25

I need a solution to this problem, would someone mind posting a link to it for a short time since I need it for the Monday for the preparation for the state competition, thanks in advance. :)

anirudnits: 2018-01-31 19:36:10

@kmkhan_014 thanks :)

Shivam Gupta: 2017-12-20 22:03:26

For those using dijkstra, another optimization that nobody seems to have mentioned is that instead of running dijkstra for each source-dest pair, store all the queries and run dijkstra only once from each source vertex till all the dest. are found

tzolkat: 2017-11-14 03:00:45

I need a bigger file to test with.

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)