#K2446. Shortest Delivery Route
Shortest Delivery Route
Shortest Delivery Route
You are given a network of intersections and roads connecting them. Each road connects two intersections bidirectionally and has an associated distance. Your task is to find the shortest delivery route from a given starting intersection to a destination intersection.
If a route exists, output the total distance and the sequence of intersections along the route. If no route exists, output Impossible
.
The problem uses Dijkstra's algorithm to determine the minimum distance path. All formulas or equations if any should be written in LaTeX format. For example, the shortest distance "d" is computed according to the principle:
$$d(v) = \min_{u \in \text{neighbors}(v)}\{ d(u) + w(u, v) \} $$inputFormat
The input is read from stdin and has the following format:
- The first two integers are
n
(number of intersections) ande
(number of roads). - The next
e
lines each contain three integersu
,v
,d
representing a road between intersectionsu
andv
with distanced
. - Finally, there are two integers:
start
andend
, the starting and destination intersections respectively.
All indices are 1-indexed.
outputFormat
Output to stdout:
- If a valid route exists, print two lines: The first line contains the total distance, and the second line contains the sequence of intersections (space-separated) forming the route.
- If no route exists, print a single line:
Impossible
.
4 4
1 2 10
2 3 20
3 4 10
4 1 40
1 3
30
1 2 3
</p>