#P2700. Isolating Enemy Forces

    ID: 15965 Type: Default 1000ms 256MiB

Isolating Enemy Forces

Isolating Enemy Forces

You are given N cities connected by N-1 roads forming a tree. Among these cities, K cities are occupied by enemy forces. Each road has a demolition cost. Your task is to remove a set of roads with the minimum total cost such that in the resulting forest, no connected component contains more than one enemy‐occupied city. This will isolate the enemy forces so that they can be attacked separately.

In other words, remove some roads such that for any two enemy cities, there is no path between them. The cost of removing a road between cities u and v is given by cost.

Note: The road network forms a tree, and the removal strategy must ensure that every connected component in the remaining graph (forest) contains at most one enemy.

The answer is the minimum total cost required to achieve this isolation. It can be shown that the answer is equal to $$\text{TotalCost} - \text{MaximumKeptCost},$$ where \(\text{MaximumKeptCost}\) is the maximum possible sum of the costs of roads that can be kept while ensuring that no connected component has more than one enemy.

inputFormat

The first line contains two integers N and K — the number of cities and the number of enemy-occupied cities.

The second line contains K distinct integers, representing the indices of enemy-occupied cities (1-indexed).

Each of the following N-1 lines contains three integers: u v cost, meaning there is a road between city u and city v with a demolition cost of cost.

outputFormat

Output a single integer — the minimum total cost required to remove some roads such that no connected component contains more than one enemy-occupied city.

sample

4 2
2 4
1 2 3
2 3 2
3 4 4
2