TSHPATH - The TurtleĀ“s Shortest Path

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:mima
Date:2004-10-21
Time limit:5.659s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:DASM Programming League 2003 (thanks to Darek Dereniowski)
a copy of SHPATH problem with 60s time limit

hide comments
2015-03-01 14:25:35 Arafat dad Khan
Ac After 40 Wcs Best Feeling in the world
2014-09-29 02:48:50 Karun
clear() function is slowing stuff down!
2014-03-24 13:51:00 Xvamp999
is this the duplicate of

http://www.spoj.com/problems/SHPATH/
2013-02-17 13:50:05 Darky
@Shubham Make sure you clear the representations of the graph before each test case. I made the same mistake.
2013-02-10 07:55:57 Shubham Depp Bansal
Something is wrong with this Question...I am getting correct answers but its saying WA.Do something
2011-03-11 15:02:01 YatsukoYin
whats the fastest way to solve this problem? I mean the fastest algo.

used C++, Dijkstra with set -> 11.** time

used C# -> TLE

So I think there is better, MUCH better solution.

Edit: I/O optimization and a iterative DP

Last edit: 2012-12-23 02:21:36
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.