Heracles
Time limit2sMemory limit64 MB
Find the shortest closed walk from city 1 that visits each of the 12 required cities at least once and returns to city 1, in a weighted undirected graph.
- Level
Medium6 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
In Ancient Greece there are cities connected by bidirectional roads. It is possible to reach any city from any other by moving along the roads. There is at most one road between any two cities, and each road connects two distinct cities. Road has length .
Heracles urgently has to perform labours as directed by King Eurystheus. The labours must be performed in certain cities of Ancient Greece. Heracles is currently in the city of Mycenae, which is not among these cities. To finish the labours as fast as possible, Heracles wants to devise an optimal travel plan, according to which he must visit the required cities and return to Mycenae in the least possible time.
Help Heracles determine the minimum time for the travel. Heracles traverses a road of length in time . Every road can be traversed any number of times in either direction, and any city can be visited any number of times. The order of visiting the cities does not matter. Time spent performing the labours does not need to be counted.
Input
The first line contains the integers and (, ).
The following lines describe the roads. The -th of them has the form << >>, meaning that the -th road connects the cities numbered and and has length (, , ). It is guaranteed that there is at most one road between any two cities, and that it is possible to reach every city from any other city.
Mycenae has number , and the cities where Heracles must perform the labours have numbers from to .
Output
Output one integer: the minimum possible time of the travel.
Hint
One of the optimal travel plans for the sample is