1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long ll; int n, m, k, hea[50005], cnt, num[50005], uu, vv, ww; bool isi[50005]; ll dp[50005][105], tmp[105]; struct Edge{ int too, nxt, val; }edge[100005]; void add_edge(int fro, int too, int val) { edge[++cnt].too = too; edge[cnt].nxt = hea[fro]; edge[cnt].val = val; hea[fro] = cnt; } void dfs(int x, int fa) { dp[x][0] = 0; if(isi[x]) { num[x] = 1; dp[x][1] = 0; } for(int i=hea[x]; i; i=edge[i].nxt) { int t=edge[i].too; if(t==fa) continue; dfs(t, x); int mx=min(k, num[x]+num[t]); for(int j=0; j<=mx; j++) tmp[j] = dp[x][j]; for(int j=0; j<=num[x]; j++) for(int l=0; l<=num[t] && j+l<=mx; l++) tmp[j+l] = min(tmp[j+l], dp[x][j]+(ll)dp[t][l]+l*(k-l)*edge[i].val); for(int j=0; j<=mx; j++) dp[x][j] = min(tmp[j], dp[x][j]); num[x] += num[t]; } } int main() { cin>>n>>m>>k; memset(dp, 0x3f, sizeof(dp)); for(int i=1; i<=m; i++) { scanf("%d", &uu); isi[uu] = true; } for(int i=1; i<n; i++) { scanf("%d %d %d", &uu, &vv, &ww); add_edge(uu, vv, ww); add_edge(vv, uu, ww); } dfs(1, -1); cout<<dp[1][k]<<endl; return 0; }
|