#K75667. Bus Route Travel Time
Bus Route Travel Time
Bus Route Travel Time
You are given a transport network of bus stops and bus routes. Your task is to determine the minimum travel time between two specified stops in the network. The travel time along a route is given and you must use these routes to travel from a start stop to a destination stop.
Formally, consider a directed graph \(G=(V,E)\), where \(V\) represents the bus stops and \(E\) represents the bus routes. Each edge \((u, v)\) has a weight \(w(u,v)\) representing the travel time. The goal for each query is to compute the minimum travel time from a given start stop \(s\) to an end stop \(t\) such that:
[ d(s,t)=\min_{path,s\to t} \sum_{(u,v)\in path} w(u,v) ]
If no path exists, output IMPOSSIBLE
.
You are required to read the input from stdin
and write the output to stdout
.
inputFormat
The input is given in the following format:
S stop_1 stop_2 ... stop_S R origin_1 destination_1 time_1 origin_2 destination_2 time_2 ... origin_R destination_R time_R Q query_start_1 query_end_1 query_start_2 query_end_2 ... query_start_Q query_end_Q
Where:
S
is the number of bus stops.- The second line contains S bus stop names separated by spaces.
R
is the number of bus routes.- Each of the next R lines represents a bus route from an origin to a destination with the travel time (an integer).
Q
is the number of queries.- Each of the next Q lines contains two bus stop names representing the query from a start stop to an end stop.
outputFormat
For each query, output on a new line the minimum travel time between the two stops. If no route exists, output IMPOSSIBLE
.
5
StopA StopB StopC StopD StopE
6
StopA StopB 10
StopB StopC 20
StopC StopD 15
StopD StopE 25
StopA StopC 30
StopA StopE 60
5
StopA StopD
StopC StopE
StopA StopE
StopD StopA
StopB StopA
45
40
60
IMPOSSIBLE
IMPOSSIBLE
</p>