Shopping
Time limit3sMemory limit128 MB
Given weighted roads and up to 10 stores, find the shortest round trip from house 0 visiting every store.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
You have just moved into a new apartment and have a long list of items to buy. Buying this many items means visiting many different stores, and you would like to minimize the amount of driving needed to buy everything on your list.
The city is a set of intersections connected by roads. Your house and every store sit at some intersection. Find the length of the shortest route that starts at your house, visits every store you need to shop at, and returns to your house.
Input
The first line contains a single integer, the number of test cases. Each test case begins with a line containing two integers and , the number of intersections and the number of roads in the city (, ). The intersections are numbered from to , and your house is at intersection . Each of the next lines contains three integers , , and , meaning that intersections and are connected by a bidirectional road of length . The next line contains a single integer , the number of stores you must visit (). Each of the following lines contains one integer, the intersection at which a store is located. Every store is reachable from your house.
Output
For each test case, output a single line containing one integer: the length of the shortest shopping trip that starts at your house, visits all the stores, and returns to your house.