Roadblocks
Time limit1sMemory limit128 MB
Find the length of the second-shortest walk from vertex 1 to vertex N in an undirected weighted graph, where walks may repeat edges.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Heap, Greedy
- Solved
- No attempts yet
Problem
Bessie has moved to a small farm and sometimes likes to walk back to visit one of her best friends. She does not want to arrive too quickly, because she enjoys the scenery along the way, so she has decided to travel the second-shortest path instead of the shortest one. Such a path is guaranteed to exist.
The countryside has intersections, numbered through , connected by bidirectional roads. Each road joins two intersections and has a positive length. Bessie starts at intersection , and her friend lives at intersection .
A path here is any walk from to : it may reuse roads or intersections, and it may even backtrack over a road it has already used. The length of a path is the sum of the lengths of the roads it uses. The second-shortest path is a path whose length is strictly greater than the length of the shortest path, yet no greater than the length of any other such path. In other words, if is the shortest achievable length, the answer is the smallest achievable length that is strictly larger than . (If several different routes share the shortest length , they all count as shortest; the second-shortest length is still the next larger achievable value.)
Constraints: and .
Input
- Line 1: two space-separated integers and .
- Lines 2 to : each line contains three space-separated integers , , and , describing a bidirectional road between intersections and with length ().
Output
- Print a single line containing the length of the second-shortest path from intersection to intersection .
Hint
In the sample, the shortest path is with length , and the next-longer path is with length , so the second-shortest length is .