Intrepid climber
InterviewTime limit3sMemory limit256 MB
Starting from the root of a weighted tree, visit all marked nodes with free descents and costly climbs at minimum total energy.
Problem
You climbed the highest mountain of your city. You want to tell all your friends about it, and you decided to start with the ones who are trying to reach the exact spot where you are standing right now.
The mountain has landmarks, and one of them is the top, where you are now. Each friend climbing the mountain is at a different landmark other than the top, and you want to visit all of them. Tracks connect pairs of landmarks so that exactly one route, meaning one sequence of consecutive tracks, goes down from the top to every other landmark. Visiting two friends at two different landmarks may force you to go down some tracks, climb others, and go down again. Going down the mountain is easy, so it costs no energy, but each time you climb a track you spend a fixed amount of energy. After visiting all your friends, you can just sit and rest.
For example, take a mountain with 6 landmarks where tracks join 1 and 2, 1 and 3, 2 and 4, 3 and 5, and 3 and 6. If your friends are at landmarks 5 and 2, you can visit both by following the order 1 ↓ 2 ↑ 1 ↓ 3 ↓ 5, where a ↓ b means that you go down a track from landmark a to landmark b, and a ↑ b means that you climb a track from landmark a to landmark b. The order 1 ↓ 3 ↓ 5 ↑ 3 ↑ 1 ↓ 2 works as well.
You are given the tracks between the landmarks, the energy required to climb each of them, and the landmarks where your friends are. Compute the minimum total amount of energy required to visit all your friends starting from the top.
Input
The first line contains two integers and , the number of landmarks and the number of friends climbing the mountain (). Landmarks are identified by distinct integers from 1 to , and landmark 1 is the top of the mountain, where you start.
Each of the next lines describes one track with three integers , and , meaning that a track goes down from landmark to landmark and that climbing it requires energy (, , , ).
The last line contains distinct integers , the landmarks where your friends are (). You may assume that the tracks are such that exactly one route goes down from the top of the mountain to each other landmark.
Output
Print one line with an integer, the minimum total amount of energy required to visit all your friends starting from the top of the mountain.