#pragma target("avx2") #pragma optimize("O3") #pragma optimize("unroll-loops") #include #include using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000000000 vector get(int start,auto &E){ vector dis(E.size(),Inf); dis[start] = 0; queue Q; Q.push(start); while(Q.size()>0){ int u = Q.front(); Q.pop(); rep(i,E[u].size()){ int v = E[u][i].first; if(dis[v]!=Inf)continue; dis[v] = dis[u] + E[u][i].second; Q.push(v); } } return dis; } struct HLD{ vector sz,parent,depth,root,pos; vector arr; HLD(vector> &E){ sz.resize(E.size(),1); parent.resize(E.size(),0); depth.resize(E.size(),0); root.resize(E.size(),0); pos.resize(E.size(),0); dfs(0,-1,E); dfs2(0,-1,E,0); } void dfs(int now,int p,vector> &E){ parent[now] = p; if(p==-1){ depth[now] = 0; } else{ depth[now] = depth[p]+1; } for(int i=0;i> &E,int r){ pos[now] = arr.size(); arr.push_back(now); root[now] = r; int maxi = 0; int ind = -1; for(int i=0;i> query(int u,int v){ vector> ret; int t = 0; while(root[u]!=root[v]){ if(depth[root[u]] <= depth[root[v]]){ ret.insert(ret.begin()+t,{pos[root[v]], pos[v]}); v = parent[root[v]]; } else{ ret.insert(ret.begin()+t,{pos[u],pos[root[u]]}); u = parent[root[u]]; t++; } } ret.insert(ret.begin()+t,{pos[u],pos[v]}); return ret; } int lca(int u,int v){ for(;;v=parent[root[v]]){ if(pos[u]>pos[v])swap(u,v); if(root[u]==root[v])return u; } } int get_distance(int u,int v){ return depth[u] + depth[v] - 2 * depth[lca(u,v)]; } }; long long C; void dfs(auto &E,auto &dp,auto &ndp,auto &dis,int now,int F,int P,int p=-1){ ndp[now] = dp[now] + C + dis[now][P]; if(F==now)ndp[now] -= C; rep(i,E[now].size()){ int to = E[now][i].first; if(to==p)continue; dfs(E,dp,ndp,dis,to,F,P,now); ndp[now] = min(ndp[now],ndp[to]); } } int main(){ int N,Q; cin>>N>>Q>>C; vector E(N,vector>()); vector EE(N,vector()); rep(i,N-1){ int u,v,l; scanf("%d %d %d",&u,&v,&l); u--;v--; E[u].emplace_back(v,l); E[v].emplace_back(u,l); EE[u].push_back(v); EE[v].push_back(u); } vector x(Q); rep(i,Q){ scanf("%d",&x[i]); x[i]--; } vector dis(N,vector()); rep(i,N){ dis[i] = get(i,E); } vector dp(N,Inf); dp[x[0]] = 0LL; rep(i,Q-1){ vector ndp(N,Inf); dfs(E,dp,ndp,dis,x[i+1],x[i],x[i+1]); rep(j,N){ ndp[j] = min(ndp[j],dp[j] + dis[x[i]][x[i+1]]); } swap(dp,ndp); } long long ans = Inf; rep(i,N)ans = min(ans,dp[i]); cout<