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
chambers, with
passageways running between them. The
passageway connects
distinct chambers
and
, and
has
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 chamber, and fortunately for
Alucard, he's already infiltrated the castle and also finds himself in
the
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
items.
Conveniently, all of these items can be found in distinct chambers of
Dracula's castle, with the
item in chamber
.
Alucard will need to travel around the castle through its passageways,
starting from the chamber, visiting all
chambers that contain
his required items (in any order), and arriving back in the
chamber
to finally wake and confront Dracula. If he chooses to travel through a
passageway that contains
monsters, he'll first need to destroy them
by casting a spell and using up
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 items while requiring him to use as few
magic points as possible. Can you help him?
In test cases worth of the points,
and
.
In test cases worth another of the points,
.
Input Specification
The first line of input consists of two space-separated integers and
.
lines follow, with the
of these lines consisting of three
space-separated integers
,
, and
(for
).
lines follow, with the
of these lines consisting of a single
integer
(for
).
Output Specification
Output one line consisting of a single integer - the minimum number of
magic points required for Alucard to collect all 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 chambers
that contain items and then returning to the
chamber, is as follows:
(
magic points)
(
magic points)
(already cleared)
(already cleared)
(
magic points)
(
magic points)
(already cleared)
(
magic points)
(already cleared)
(already cleared)
The total number of magic points required on this route is
.
Comments