#P4784. Connect the Major Cities in Byteland

    ID: 18028 Type: Default 1000ms 256MiB

Connect the Major Cities in Byteland

Connect the Major Cities in Byteland

In \(Byteland\) there are \(n\) cities and \(k\) major cities frequently visited by kings. There are \(m\) bidirectional roads connecting pairs of cities. Unfortunately, these roads are in such poor condition that kings cannot travel at full speed.

Each road has a known repair cost \(w\). Your task is to choose a subset of roads to repair so that all the \(k\) major cities become connected via the repaired roads, and the total repair cost is minimized.

If it is impossible to connect all the major cities, output \(-1\).

Input: The first line contains three integers \(n\), \(m\) and \(k\). The second line contains \(k\) distinct integers indicating the indices of the major cities (cities are numbered from \(1\) to \(n\)). Each of the next \(m\) lines contains three integers \(u\), \(v\) and \(w\), representing a road between cities \(u\) and \(v\) with repair cost \(w\).

Output: Output a single integer representing the minimum total repair cost to connect all the major cities, or \(-1\) if it is impossible.

inputFormat

The input begins with a line containing three integers \(n\), \(m\), and \(k\): the number of cities, the number of roads, and the number of major cities.

The second line contains \(k\) distinct integers, each in the range \([1, n]\), representing the indices of the major cities.

Then follow \(m\) lines, each with three integers \(u\), \(v\), and \(w\), indicating that there is a road connecting city \(u\) and city \(v\) with a repair cost \(w\).

outputFormat

Output one integer: the minimum total cost required to repair roads so that all \(k\) major cities are connected. If connecting them is impossible, output \(-1\).

sample

4 4 2
1 3
1 2 3
2 3 4
1 4 10
3 4 5
7

</p>