WC '16 Contest 1 S2 - Alucard's Quest

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.8s
Memory limit: 64M

Author:
Problem type
Woburn Challenge 2016-17 Round 1 - Senior Division

What a horrible night to have a curse! Alucard has returned to the ancient castle of his evil father, Dracula, determined to wake him from his slumber and then destroy him once and for all. However, something tells him that it won't be easy - though Dracula remains fast asleep in his coffin for now, his monstrous servants are scattered throughout the castle, armed to the teeth and hungry for blood.

The castle consists of N (1 \le N \le 200\,000) chambers, with N-1 passageways running between them. The i^{th} passageway connects distinct chambers A_i and B_i (1 \le A_i, B_i \le N), and has M_i (1 \le M_i \le 5\,000) monsters in it. It's possible to reach any chamber from any other chamber by following a sequence of one or more passageways - in other words, the system of chambers and passageways forms a tree structure when modelled as a graph.

Dracula's resting place is in the 1^{st} chamber, and fortunately for Alucard, he's already infiltrated the castle and also finds himself in the 1^{st} chamber! However, he's realized that he's not quite ready to battle Dracula yet. In order to stand a chance, Alucard will surely need some holy water, stronger weapons (a whip should come in handy), a wider range of magical spells to cast, and of course an oak stake to plunge into his father's heart and finish him off permanently. In particular, Alucard will first need to gather K (1 \le K < N) items. Conveniently, all of these items can be found in distinct chambers of Dracula's castle, with the i^{th} item in chamber C_i (2 \le C_i \le N).

Alucard will need to travel around the castle through its passageways, starting from the 1^{st} chamber, visiting all K chambers that contain his required items (in any order), and arriving back in the 1^{st} chamber to finally wake and confront Dracula. If he chooses to travel through a passageway that contains m monsters, he'll first need to destroy them by casting a spell and using up m of his "magic points". That passageway will then be permanently cleared of monsters, so he'll be able to freely travel through it any number of times afterwards.

Conserving magic points for his battle with Dracula is vital, so Alucard will need to carefully plan out a route through the castle which will allow him to collect all K items while requiring him to use as few magic points as possible. Can you help him?

In test cases worth 3/20 of the points, N \le 1\,000 and K = 1.
In test cases worth another 7/20 of the points, N \le 1\,000.

Input Specification

The first line of input consists of two space-separated integers N and K.
N-1 lines follow, with the i^{th} of these lines consisting of three space-separated integers A_i, B_i, and M_i (for i = 1 \dots N-1).
K lines follow, with the i^{th} of these lines consisting of a single integer C_i (for i = 1 \dots K).

Output Specification

Output one line consisting of a single integer - the minimum number of magic points required for Alucard to collect all K items and return to Dracula's chamber.

Sample Input

7 4
1 2 5
1 7 2
2 4 3
2 5 8
5 6 1
7 3 10
4
5
3
7

Sample Output

28

Sample Explanation

One optimal route that Alucard can take, passing through all 4 chambers that contain items and then returning to the 1^{st} chamber, is as follows:

  • 1 \to 7 (2 magic points)
  • 7 \to 3 (10 magic points)
  • 3 \to 7 (already cleared)
  • 7 \to 1 (already cleared)
  • 1 \to 2 (5 magic points)
  • 2 \to 4 (3 magic points)
  • 4 \to 2 (already cleared)
  • 2 \to 5 (8 magic points)
  • 5 \to 2 (already cleared)
  • 2 \to 1 (already cleared)

The total number of magic points required on this route is 2 + 10 + 5 + 3 + 8 = 28.


Comments

There are no comments at the moment.