Volunteer Camp
Time limit2sMemory limit128 MB
Starting from each house in a weighted tree, find the shortest truck route that visits K marked houses without driving back.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
A village hit by a flood is opening a volunteer camp. The village has houses numbered to , joined by roads, so exactly one route runs between any two houses. Each road has a fixed time a truck needs to drive along it. The camp goes in the garden of one house, and the manager has not chosen that house yet.
Mirko drives the truck. His job is to carry volunteer teams from the camp to the house where each team works. Every team fits in the truck at the same time. There are teams and each team goes to a different house.
Mirko loads all teams at the camp, then drops them off in an order he picks himself. After the last team gets out, he stays in that house and helps, so he never drives back to the camp.
For every house, compute the minimal time Mirko needs to deliver all the teams when the camp is in that house.
Input
The first line contains two integers and (, ).
Each of the next lines contains three integers , , , meaning that a truck needs time to drive the two way road between house and house (, ).
Each of the next lines contains the number of the house one team is going to, one number per line. The numbers are distinct.
Output
Print lines. Line contains the minimal time Mirko needs to deliver all the teams when the camp is in house .
Note
Look at the first example. Starting from house , Mirko can drive to houses , , , in that order. Starting from house , he can drive to houses , , .