Blazing New Trails
Time limit2sMemory limit512 MB
Choose a spanning tree of a graph whose edges each join a marked or unmarked vertex, with exactly w marked-unmarked edges, minimizing total cost.
- Level
Hard8 of 10
- Topics
- Minimum spanning tree, Graph, Sorting, Greedy
- Solved
- No attempts yet
Problem
Your state has just purchased a large, unspoiled tract of land and wants to turn it into a nature park with hiking trails. The land has places of interest that guests may want to hike to, and of them are special. The state wants to connect these places with hiking trails.
There are candidate trails. Each one directly connects two places of interest and has its own cost. The chosen trails must satisfy these constraints:
- There must be exactly one way to hike from any place to any other place.
- Exactly of the chosen trails must directly connect a special place with a regular place.
The state wants to minimize the total cost of blazing the trails. Find that minimum.
Input
The input consists of a single test case.
The first line contains four integers , , and , where () is the number of places, () is the number of candidate direct trails between places, () is the number of special places, and () is the number of special-regular direct trails the state wants to blaze. The places are numbered to .
Each of the next lines contains a single integer (), the number of a special place. These values are distinct and given in ascending order.
Each of the next lines describes a candidate trail with three integers , and : the trail would connect places and (, ) and would cost (). No two places have more than one candidate trail between them, and a trail from to is the same as a trail from to .
Output
Print a single integer: the minimum total cost of blazing trails that satisfy the constraints, or if it is not possible.