Task DescriptionDiscussion (0)
Task :: Harbingers
Once upon a time, there were N medieval towns in the beautiful Moldavian territory, uniquely numbered from 1 through N. The town numbered with 1 was the capital city. The towns were connected by N-1 bidirectional roads, each road having a length expressed in kilometers. There was a unique way to travel between any pair of towns without going through a town twice (i.e. the graph of roads was a tree).

When a town was attacked, the situation had to be reported as soon as possible to the capital. The message was carried by harbingers, one of which resided in each town. Each harbinger was characterized by the amount of time required to start the journey and by his constant speed (expressed in minutes per kilometer) after departure.

The message from a town was always carried on the unique shortest path to the capital. Initially, the harbinger from the attacked town carried the message. In each town that he traversed, a harbinger had two options: either go to the next town towards the capital, or leave the message to the harbinger from this town. The new harbinger applied the same algorithm as above. Overall, a message could be carried by any number of harbingers before arriving in the capital.

Your task is to find, for each town, the minimum time required to send a message from that town to the capital.

The first line of the input file harbingers.in contains one integer N, the number of towns in Moldavia. Each of the following N-1 lines contains three integers u v d, separated by one space, describing a road of length d kilometers between towns numbered with u and v. Subsequently, N-1 pairs of integers follow, one per line. The ith pair, Si Vi, describes the characteristics of the harbinger in the (i+1)th town: Si is the number of minutes to prepare for the journey, and Vi is the number of minutes needed to travel one kilometer. There is no harbinger in the capital.

The output file harbingers.out should consist of exactly one line containing N-1 integers. The ith number represents the minimum time, in minutes, required to send a message from the (i+1)th town to the capital.

• 3 ≤ N ≤ 100 000
• 0 ≤ Si ≤ 109
• 1 ≤ Vi ≤ 109
• The length of each road will not exceed 10 000
• For 20% of the tests, N ≤ 2 500
• For 50% of the tests, each town will have at most 2 adjacent roads (i.e., the graph of roads will be a line)

1 2 20
2 3 12
2 4 1
4 5 3
26 9
1 10
500 2
2 30

206 321 542 328


The roads and their lengths are shown in the image on the top. The start-up time and speed of the harbingers are written between brackets. The minimum time to send a message from town 5 to the capital is achieved as follows. The harbinger from town 5 takes the message and leaves the town after 2 minutes. He walks 4 kilometers in 120 minutes before arriving in town 2. There he leaves the message to the harbinger from that town. The second harbinger requires 26 minutes to start the journey and walks for 180 minutes before arriving to the capital.
The total time is therefore 2 + 120 + 26 + 180 = 328.
Submit Solution
Available Languages
Task info
Time:0.5 sec.
Memory:32 MB
AddedBy: harta01
Source:CEOI 2009
Task Ratings

5 (13 votes)

5 (10 votes)
Acceptance Rate
Recent Submissions
Fastest Solutions
IAmNomad 0.417 s.
mateuscd11 0.443 s.
andmej 0.457 s.
vjudge6 0.5 s.
stjepang 0.834 s.
flashmt 0.906 s.
leandro 0.961 s.
dfistric 0.969 s.
paulica 0.994 s.
kisic14 1.006 s.
Solved By