Sirens
Time limit3sMemory limit1024 MB
Given a weighted tree where siren i activates every room within distance d_i, find the fewest sirens to turn on by hand so all sirens turn on.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Graph, DFS
- Solved
- No attempts yet
Statement
An underground bunker has rooms connected by corridors. Each corridor joins two different rooms and has a fixed length. The bunker is built so that you can walk from any room to any other room . There is exactly one path between two rooms that does not use the same corridor twice. The sum of the lengths of the corridors on that path is called the distance between rooms and , written .
Each room has a siren and a sound sensor that turns it on. A siren in room activates the sensor in every room with , where is the power of that siren. So turning on the siren in room automatically turns on the siren in every room with . Each siren that turns on can in turn trigger other sirens, and so on.
In an emergency, some sirens must be turned on by hand. Their sound then turns on the sirens in other rooms automatically. Safety rules require choosing a set of sirens to turn on by hand so that, in the end, the sirens in all rooms turn on automatically.
Write a program that finds the minimum number of sirens in a set that satisfies the safety rules.
Input
The first line contains one integer , the number of rooms.
The second line contains integers , where is the maximum distance at which the siren in room activates the sensors ().
Each of the next lines describes a corridor with three integers , , . Here and are the numbers of the two different rooms connected by corridor , and is the length of that corridor (; ).
Output
Print one integer, the minimum number of sirens that must be turned on by hand.
Constraints
Hint
In the sample test, the siren in room 4 turns on the siren in room 5, which in turn turns on the sirens in rooms 6 and 7. The siren in room 2 turns on the siren in room 3. The siren in room 8 turns on the sirens in rooms 1, 9, and 10.
