#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include using namespace std; #include using namespace atcoder; #define REP(i,n) for(int i = 0; i < (int)n; i++) #define RREP(i,n) for(int i = (int)n-1; i >= 0; i--) #define LREP(i,n) for(LL i = 0; i < (LL)n; i++) #define Vi vector #define Vl vector #define P pair #define LP pair #define T3 tuple #define T4 tuple #define INF 1000000007 #define SIZE 3010 #define MOD 998244353 typedef long long LL; LL N, Q, C; vector E[SIZE]; LL X[SIZE]; LL dist[SIZE][SIZE]; LL dp[SIZE][SIZE]; void init() { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) dist[i][j] = -1; dist[i][i] = 0; queue que; que.push(i); while (que.size()) { int u = que.front(); que.pop(); for (auto e : E[u]) { if (dist[i][e.first] == -1) { dist[i][e.first] = dist[i][u] + e.second; que.push(e.first); } } } } } int main() { cin >> N >> Q >> C; REP(i, N - 1) { LL u, v, l; cin >> u >> v >> l; E[u].push_back(LP(v, l)); E[v].push_back(LP(u, l)); } REP(i, Q) cin >> X[i + 1]; init(); REP(i, Q + 1) REP(j, N + 1) dp[i][j] = 1e18; for (int q = 1; q < Q; q++) { Vl A, B; int t = X[q + 1], s = X[q]; while (t != s) { for (auto e : E[t]) { if (dist[s][e.first] == dist[s][t] - e.second) { A.push_back(t); t = e.first; break; } } } A.push_back(s); t = X[q + 1]; for (int i = 1; i <= N; i++) { dp[q][i] = dp[q - 1][i] + dist[s][t]; } for (auto u : A) { B.push_back(C + dp[q - 1][u] + dist[u][t]); } reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); REP(i, B.size() - 1) { if (B[i + 1] > B[i]) { B[i + 1] = B[i]; } } REP(i, A.size()) { dp[q][A[i]] = min(dp[q][A[i]], B[i]); if (q == 1) dp[q][A[i]] = dist[s][t]; } } LL ans = 1e18; for (int i = 1; i <= N; i++) ans = min(ans, dp[Q - 1][i]); cout << ans << endl; }