#P6192. 【模板】最小斯坦纳树

【模板】最小斯坦纳树

Problem Description

You are given an undirected connected graph G=(V,E)G=(V,E) with nn vertices and mm weighted edges.

You are also given a set SS containing kk vertices. Choose a subgraph G=(V,E)G'=(V',E') of GG such that:

  1. SVS\subseteq V'.
  2. GG' is connected.
  3. The sum of edge weights in EE' is minimized.

You only need to output the sum of edge weights in EE'.

Input Format

The first line contains three integers n,m,kn,m,k, representing the number of vertices, the number of edges in GG, and the size of SS.

The next mm lines each contain three integers u,v,wu,v,w, indicating an undirected edge between vertices uu and vv with weight ww.

The next line contains kk distinct positive integers, representing the elements of SS.

Output Format

The first line contains one integer, the minimum possible sum of edge weights in EE'.

7 7 4
1 2 3
2 3 2
4 3 9
2 6 2
4 5 3
6 5 2
7 6 4
2 4 7 5

11

Hint

[Sample Explanation]

The graph in the sample is shown in the figure below. The red vertices are the elements in SS, and the red edges are the elements in EE'. In this case, the sum of all edge weights in EE' is 2+2+3+4=112+2+3+4=11, which is the minimum.


[Constraints]

For 100%100\% of the testdata, $1\leq n\leq 100,\ \ 1\leq m\leq 500,\ \ 1\leq k\leq 10,\ \ 1\leq u,v\leq n,\ \ 1\leq w\leq 10^6$.

The given undirected graph is guaranteed to be connected, but there may be multiple edges and self-loops.

Translated by ChatGPT 5