## https://yukicoder.me/problems/no/1308 from collections import deque def main(): N, Q, C = map(int, input().split()) next_nodes = [[] for _ in range(N)] edges = {} for _ in range(N-1): a, b, l = map(int, input().split()) next_nodes[a-1].append((b - 1, l)) next_nodes[b-1].append((a - 1, l)) edges[(a - 1, b - 1)] = l edges[(b - 1, a - 1)] = l X = list(map(int, input().split())) path_array = [] for i in range(Q - 1): s_x = X[i] - 1 e_x = X[i + 1] - 1 queue = deque() queue.append(s_x) parents = [-2] * N parents[s_x] = -1 while len (queue) > 0: v = queue.popleft() for next_node, _ in next_nodes[v]: if next_node == parents[v]: continue parents[next_node] = v queue.append(next_node) path = [] v = e_x while v != s_x: path.append(v) v = parents[v] path.append(s_x) path.reverse() path_array.append(path) # 下手に走った時の走行時間の累積 cum_dist_array = [] cum_dist = 0 for path in path_array: cum_dists = [] prev_p = -1 for p in path: if prev_p != -1: cum_dist += edges[(prev_p, p)] cum_dists.append(cum_dist) prev_p = p cum_dist_array.append(cum_dists) dp = [ [float("inf")] * len(path_array[q]) for q in range(Q - 1)] dp[0][0] = 0 last_passed = [-1] * N last_passed[path_array[0][0]] = (0, 0) for q in range(Q - 1): for i in range(1, len(path_array[q])): dp[q][i] = dp[q][i - 1] + edges[(path_array[q][i - 1], path_array[q][i])] if last_passed[path_array[q][i]] != -1: q_, i0 = last_passed[path_array[q][i]] d = cum_dist_array[q][0] - cum_dist_array[q_][i0] dp[q][i] = min(dp[q][i], dp[q_][i0] + d + C) last_passed[path_array[q][i]] = (q, i) if q < Q - 2: dp[q + 1][0] = dp[q][-1] last_passed[path_array[q + 1][0]] = (q + 1, 0) print(dp[-1][-1]) if __name__ == "__main__": main()