luogu#P2700. 逐个击破

逐个击破

Background

On the Pingjin battlefield of the three major campaigns, the Fu Zuoyi group deployed a long, thin defensive line centered on Beiping and Tianjin, stretching from Tangshan in the east to Zhangjiakou in the west along the railway. They planned to flee south by sea or retreat westward in case of collapse. To annihilate the enemy in place and prevent their escape, the commander formulated a strategy to first cut off the enemy’s retreat routes at both ends and then eliminate them one by one. Following the strategic thinking of a great military leader, as a wise corps commander, you encounter a similar battlefield situation.

Problem Description

There are NN cities, among which KK are occupied by enemy corps. There are N1N - 1 roads connecting the NN cities. The cost to destroy any given road is known. You are given the KK cities occupied by the enemy and the destruction cost of every road. Please compute the minimum total cost to isolate these KK enemy corps from each other, so that in the second step they can be eliminated one by one.

Input Format

  • The first line contains two positive integers NN and KK.
  • The second line contains KK integers, indicating which cities are occupied by the enemy.
  • The next N1N - 1 lines each contain three positive integers a,b,ca, b, c, indicating there is a road between city aa and city bb, and the cost to destroy it is cc. City indices start from 00.

Output Format

Output a single line with one integer, the minimum total cost.

5 3
1 2 4
1 0 4
1 3 8
2 1 1
2 4 3
4

Hint

Constraints:

  • For 10%10\% of the testdata, N10N \le 10.
  • For 100%100\% of the testdata, 2N1052 \le N \le 10^5, 2KN2 \le K \le N, 1c1061 \le c \le 10^6.

Translated by ChatGPT 5