Cities
InterviewTime limit4sMemory limit256 MB
Given a weighted undirected graph, pick a minimum-cost set of edges so that all k special cities (k at most 10) end up in one connected component.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Dynamic programming, Graph, Union-find
- Solved
- No attempts yet
Problem
Apple Country has cities, and of them are important cities that King Gusagwa visits often. There are roads connecting pairs of cities, and the cities are numbered through .
One day a storm hit Apple Country. It was the largest natural disaster since the country was founded, and every road became unusable. The ministers decided to rebuild roads so that traffic between cities can continue, and they surveyed the cost of rebuilding each road.
King Gusagwa had lived a life of luxury and indulgence, so the treasury was empty and not every road could be rebuilt. For now the ministers want to rebuild a set of roads that connects the important cities to each other, meaning that from any important city you can reach every other important city.
Find the minimum cost of rebuilding roads so that the important cities are connected to each other.
Input
The first line contains the number of cities , the number of important cities , and the number of roads , separated by spaces.
The second line contains integers separated by spaces. They are distinct integers between and , and they give the numbers of the important cities.
Each of the next lines describes one road with three integers , , . The road connects city and city , and rebuilding that road costs .
Every city is connected by roads. Several roads may connect the same pair of cities.
Output
Print the minimum cost of rebuilding roads so that the important cities are connected to each other, as a single integer.
Constraints
- ,