Apple Delivery
Time limit1sMemory limit128 MB
Given a weighted undirected graph, find the shortest round trip from a start node that visits two given nodes in either order.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Greedy, Implementation
- Solved
- No attempts yet
Problem
Bessie has two crisp red apples to deliver to two of her friends in the herd.
The pastures are numbered through () and are connected by bidirectional cowpaths (). Each cowpath connects two distinct pastures and and has length . No cowpath leads from a pasture to itself. The sum of all cowpath lengths does not exceed . It is always possible to travel from any pasture to any other pasture.
Bessie starts at pasture and must deliver both apples by visiting the two distinct pastures and in either order. The three pastures , , and are all distinct. She may reuse pastures and cowpaths she has already visited, and the distance traveled is the sum of the lengths of all cowpaths she uses.
Find the minimum total distance Bessie must travel to deliver both apples.
The map below illustrates pasture numbers (in brackets) together with the cowpaths and their lengths.
3 2 2
[1]-----[2]------[3]-----[4]
\ / \ /
7\ /4 \3 /2
\ / \ /
[5]-----[6]------[7]
1 2
Input
The first line contains five space-separated integers , , , , and .
Each of the next lines describes cowpath with three integers , , and : the two pastures it connects and its length.
Output
Print, on a single line, the minimum total distance Bessie must travel to deliver both apples.