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.
Hard8Minimum spanning treeGraphSortingGreedyNo attempts yetTime limit2sMemory limit512 MBYour 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 n places of interest that guests may want to hike to, and k of them are special. The state wants to connect these places with hiking trails.
There are m candidate trails. Each one directly connects two places of interest and has its own cost. The chosen trails must satisfy these constraints:
The state wants to minimize the total cost of blazing the trails. Find that minimum.
The input consists of a single test case.
The first line contains four integers n, m, k and w, where n (2≤n≤2×105) is the number of places, m (1≤m≤5×105) is the number of candidate direct trails between places, k (1≤k<n) is the number of special places, and w (1≤w≤n−1) is the number of special-regular direct trails the state wants to blaze. The places are numbered 1 to n.
Each of the next k lines contains a single integer s (1≤s≤n), the number of a special place. These values are distinct and given in ascending order.
Each of the next m lines describes a candidate trail with three integers a, b and c: the trail would connect places a and b (1≤a,b≤n, a=b) and would cost c (1≤c≤105). No two places have more than one candidate trail between them, and a trail from a to b is the same as a trail from b to a.
Print a single integer: the minimum total cost of blazing trails that satisfy the constraints, or −1 if it is not possible.