#include using namespace std; template void chmin(T &a, T b) { if (b < a) { a = b; } } using graph = vector>>; void dfs(const graph &to, vector &dist, int v, int par) { for (auto [next, l] : to[v]) { if (next == par) continue; dist[next] = dist[v] + l; dfs(to, dist, next, v); } } int main() { constexpr int64_t inf = 1e18; int64_t n, q, c; cin >> n >> q >> c; graph to(n); for (int i = 0; i < n - 1; i++) { int64_t u, v, l; cin >> u >> v >> l; u--; v--; to[u].emplace_back(v, l); to[v].emplace_back(u, l); } vector x(q); for (int i = 0; i < q; i++) { cin >> x[i]; x[i]--; } vector dist(n, vector(n, int64_t())); for (int i = 0; i < n; i++) { dfs(to, dist[i], i, -1); } vector dp(q, vector(n, inf)); dp.at(0).at(x.at(0)) = 0; for (int i = 1; i < q; i++) { int64_t mincost_prev = *min_element(dp[i - 1].begin(), dp[i - 1].end()); vector with_teleport(n, inf); for (int j = 0; j < n; j++) { chmin(with_teleport[j], dp[i - 1][j] + c + dist[j][x[i]]); } for (int j = 0; j < n; j++) { chmin(dp[i][j], dp[i - 1][j] + dist[x[i - 1]][x[i]]); if (dist[x[i - 1]][j] + dist[j][x[i]] != dist[x[i - 1]][x[i]]) continue; chmin(dp[i][j], mincost_prev + dist[x[i - 1]][x[i]]); for (int k = 0; k < n; k++) { if (dist[j][k] + dist[k][x[i]] != dist[j][x[i]]) continue; chmin(dp[i][k], with_teleport[j]); } } } cout << dp[q - 1][x[q - 1]] << endl; }