#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template struct Edge { int src, dst; CostType cost; Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {} inline bool operator<(const Edge &x) const { return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src; } inline bool operator<=(const Edge &x) const { return !(x < *this); } inline bool operator>(const Edge &x) const { return x < *this; } inline bool operator>=(const Edge &x) const { return !(*this < x); } }; template struct HLD { std::vector parent, subtree, id, inv, head; std::vector cost; HLD(const std::vector>> &graph, int root = 0) : graph(graph) { int n = graph.size(); parent.assign(n, -1); subtree.assign(n, 1); id.resize(n); inv.resize(n); head.resize(n); dfs1(root); head[root] = root; int now_id = 0; dfs2(root, now_id); } template void v_update(int u, int v, Fn f) const { while (true) { if (id[u] > id[v]) std::swap(u, v); f(std::max(id[head[v]], id[u]), id[v] + 1); if (head[u] == head[v]) return; v = parent[head[v]]; } } template T v_query(int u, int v, F f, G g, const T UNITY) const { T left = UNITY, right = UNITY; while (true) { if (id[u] > id[v]) { std::swap(u, v); std::swap(left, right); } left = g(left, f(std::max(id[head[v]], id[u]), id[v] + 1)); if (head[u] == head[v]) break; v = parent[head[v]]; } return g(left, right); } template void subtree_v_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver]); } template T subtree_v_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver]); } template void e_update(int u, int v, Fn f) const { while (true) { if (id[u] > id[v]) std::swap(u, v); if (head[u] == head[v]) { f(id[u], id[v]); break; } else { f(id[head[v]] - 1, id[v]); v = parent[head[v]]; } } } template T e_query(int u, int v, F f, G g, const T UNITY) const { T left = UNITY, right = UNITY; while (true) { if (id[u] > id[v]) { std::swap(u, v); std::swap(left, right); } if (head[u] == head[v]) { left = g(left, f(id[u], id[v])); break; } else { left = g(left, f(id[head[v]] - 1, id[v])); v = parent[head[v]]; } } return g(left, right); } template void subtree_e_update(int ver, Fn f) const { f(id[ver], id[ver] + subtree[ver] - 1); } template T subtree_e_query(int ver, Fn f) const { return f(id[ver], id[ver] + subtree[ver] - 1); } int lca(int u, int v) const { while (true) { if (id[u] > id[v]) std::swap(u, v); if (head[u] == head[v]) return u; v = parent[head[v]]; } } private: std::vector>> graph; void dfs1(int ver) { for (Edge &e : graph[ver]) { if (e.dst != parent[ver]) { parent[e.dst] = ver; dfs1(e.dst); subtree[ver] += subtree[e.dst]; if (subtree[e.dst] > subtree[graph[ver].front().dst]) std::swap(e, graph[ver].front()); } } } void dfs2(int ver, int &now_id) { id[ver] = now_id++; inv[id[ver]] = ver; for (const Edge &e : graph[ver]) { if (e.dst != parent[ver]) { head[e.dst] = (e.dst == graph[ver].front().dst ? head[ver] : e.dst); cost.emplace_back(e.cost); dfs2(e.dst, now_id); } } } }; template struct LCADoubling { std::vector depth; std::vector dist; LCADoubling(const std::vector>> &graph) : graph(graph) { n = graph.size(); depth.resize(n); dist.resize(n); while ((1 << table_h) <= n) ++table_h; parent.resize(table_h, std::vector(n)); } void build(int root = 0) { is_built = true; dfs(-1, root, 0, 0); for (int i = 0; i + 1 < table_h; ++i) for (int ver = 0; ver < n; ++ver) { parent[i + 1][ver] = parent[i][ver] == -1 ? -1 : parent[i][parent[i][ver]]; } } int query(int u, int v) const { assert(is_built); if (depth[u] > depth[v]) std::swap(u, v); for (int i = 0; i < table_h; ++i) { if ((depth[v] - depth[u]) >> i & 1) v = parent[i][v]; } if (u == v) return u; for (int i = table_h - 1; i >= 0; --i) { if (parent[i][u] != parent[i][v]) { u = parent[i][u]; v = parent[i][v]; } } return parent[0][u]; } CostType distance(int u, int v) const { assert(is_built); return dist[u] + dist[v] - dist[query(u, v)] * 2; } private: bool is_built = false; int n, table_h = 1; std::vector>> graph; std::vector> parent; void dfs(int par, int ver, int now_depth, CostType now_dist) { depth[ver] = now_depth; dist[ver] = now_dist; parent[0][ver] = par; for (const Edge &e : graph[ver]) { if (e.dst != par) dfs(ver, e.dst, now_depth + 1, now_dist + e.cost); } } }; int main() { int n, q, c; cin >> n >> q >> c; vector>> graph(n); REP(_, n - 1) { int u, v, l; cin >> u >> v >> l; --u; --v; graph[u].emplace_back(u, v, l); graph[v].emplace_back(v, u, l); } vector x(q); REP(i, q) cin >> x[i], --x[i]; HLD hld(graph); LCADoubling lca(graph); lca.build(); vector dp(n, LINF); dp[x[0]] = 0; FOR(i, 1, q) { vector> ins(n + 1), era(n + 1); REP(v, n) { ll t = dp[v] + c + lca.distance(v, x[i]); hld.v_update(v, x[i], [&](int a, int b) { ins[a].emplace_back(t); era[b].emplace_back(t); }); } ll thr = *min_element(ALL(dp)) + lca.distance(x[i - 1], x[i]); hld.v_update(x[i - 1], x[i], [&](int a, int b) { ins[a].emplace_back(thr); era[b].emplace_back(thr); }); vector nx(n, LINF); map mp; REP(j, n) { for (ll e : ins[j]) ++mp[e]; for (ll e : era[j]) { --mp[e]; if (mp[e] == 0) mp.erase(e); } if (!mp.empty()) nx[j] = mp.begin()->first; } REP(j, n) { dp[j] += lca.distance(x[i - 1], x[i]); chmin(dp[j], nx[hld.id[j]]); } } cout << *min_element(ALL(dp)) << '\n'; return 0; }