This page is still under construction.

Parts of this page are still being built. What you see may change.

Journey

Interview

Time limit1sMemory limit128 MB

Summary
Given a weighted tree, a start city k, and a set of target cities, find the length of the shortest walk from k that visits every target at least once.
Level

Medium6 of 10

Topics
Tree, DFS, Graph, Greedy
Solved
No attempts yet

Statement

There are nn cities in Byteland (numbered from 11 to nn), connected by bidirectional roads. There are only n−1n-1 roads, yet they connect the cities so that it is possible to travel from any city to any other city (in other words, the cities and roads form a tree).

A traveller named Byterider arrived in city number kk. He plans a journey that starts in city kk and passes through the cities m1,m2,…,mjm_1, m_2, \dots, m_j he wants to visit (in any order). These city numbers are all distinct and all different from kk. Byterider has only a limited amount of money, so he wants to visit all the planned cities using the shortest possible path (starting in city kk). A path is a single road or a sequence of roads, where each next road starts in the city where the previous one ends. Determine the length of the shortest path for Byterider's journey.

Write a program which

  • reads from standard input:
    • the description of the roads connecting the cities of Byteland,
    • the number of the city where Byterider arrived,
    • the list of cities Byterider would like to visit,
  • computes the minimum length of Byterider's journey,
  • writes the result to standard output.

Input

The first line contains two integers nn and kk separated by a single space (2≤n≤500002 \le n \le 50000, 1≤k≤n1 \le k \le n), where nn is the number of cities and kk is the number of the first city on Byterider's path. Each of the next n−1n-1 lines describes one road. The ii-th of these lines (1≤i≤n−11 \le i \le n-1) contains three integers aia_i, bib_i, and did_i separated by single spaces (1≤ai,bi≤n1 \le a_i, b_i \le n, 1≤di≤10001 \le d_i \le 1000); aia_i and bib_i are the cities connected by the road, and did_i is its length. The next line contains one integer jj, the number of cities Byterider would like to visit (1≤j≤n−11 \le j \le n-1). The following line contains jj distinct integers mim_i separated by single spaces, the numbers of the cities Byterider wants to visit (1≤mi≤n1 \le m_i \le n, mi≠km_i \ne k).

Output

Print a single integer: the length of the shortest path for Byterider's journey.

Hint

Examples2

  1. Example 1

    Input
    4 2
    1 2 1
    4 2 2
    2 3 3
    2
    1 3
    
    Expected output
    5
    
  2. Example 2

    Input
    2 1
    1 2 5
    1
    2
    
    Expected output
    5