National Olympiad in Informatics, China, 2014
Day 2, Problem 3 - Ticket Purchase
This summer, NOI is celebrating her 30th birthday in city SZ. The OIers from n cities across the nation will arrive in city SZ to participate in the celebration.
The n cities in the nation form a tree rooted at city SZ. Each city is connected to its parent by a road. For the sake of simplicity, we have numbered the n cities with unique integers from 1 to n. City SZ is numbered 1. Outside of city SZ itself, any city v will have a parent denoted by fv, connected by a road of length sv.
Traveling from city v to city SZ can be done in the following way: Select an ancestor a of city v, pay the ticket cost, and take the public transit to city a. Then, pick ancestor b of city a, pay the ticket, and travel there. Repeat this process until city SZ is reached.
For any city v, we place a limit lv on the distance of the public transit. For an ancestor a of v, if and only if the total distance of all the roads between a and v does not exceed lv, then it's possible to go straight to city a from v by buying one ticket. For each city v, we can also provide two nonnegative integers pv and qv as the ticket price parameters. If we let the total distance of all roads from v to a equal d, then the ticket cost to travel from v to a is d·pv + qv.
OIers from all the cities wish to reach city SZ by themselves with the smallest total cost. Your task is to tell the OIers from each city the minimum possible cost they will need to pay to reach city SZ.
The first line of input contains two nonnegative integers n and t, respectively representing the number of cities and the type of the test case (explained below).
Lines 2 to n of the input will each describe a city outside of city SZ. Line v will contain five nonnegative integers fv, sv, pv, qv, and lv, respectively representing the parent of city v, the length of the road to its parent, the ticket price parameters, and the distance limit.
Note: the input will not contain city SZ which is numbered 1. Line 2 to line n will respectively represents the cities numbered from 2 to n.
Output n − 1 lines, each line containing a single integer. Line v should indicate the minimum total ticket cost if an OIer wanted to travel from city v + 1 to city SZ.
Note again: the output should not include city SZ which is numbered 1.
7 3 1 2 20 0 3 1 5 10 100 5 2 4 10 10 10 2 9 1 100 10 3 5 20 100 10 4 4 20 0 10
40 150 70 149 300 150
Refer to the diagram below:
The paths to travel to SZ from every city is outlined as follows (where arrows represent a single, direct trip):
City 2: Only 2→1 is possible, for a cost of 2×20 + 0 = 40.
City 3: Only 3→1 is possible, for a cost of 5×10 + 100 = 150.
City 4: Since 4 + 2 = 6 ≤ l4 = 10, one route is 4→1. If this is selected, then the cost will be (4 + 2)×10 + 10 = 70. If instead the route 4→2→1 is chosen, then the cost will be (4×10 + 10) + (2×20 + 0) = 90. Thus, the best route is 4→1.
City 5: Only 5→2→1 is possible, for a cost of (9×1 + 100) + (2×20 + 0) = 149. 5→1 is not valid – since l5 = 10, and city 5 to city 1's total distance is 9 + 2 = 11 > l5, 5 cannot directly reach 1.
City 6: If 6→1 is selected, the cost will be (5 + 5)×20 + 100 = 300. If 6→3→1 is selected, the cost will be (5×20 + 100) + (5×10 + 100) = 350. Thus, the best route is 6→1.
City 7: Choose 7→4→1 for a cost of (4×20 + 0) + ((4 + 2)×10 + 10) = 150. All other methods are suboptimal to this.
All of the test cases satisfy 0 ≤ pv ≤ 106, 0 ≤ qv ≤ 1012, 1 ≤ fv < v, and 0 < sv ≤ lv ≤ 2×1011. Also, the total distance between any city and city SZ will not exceed 2 × 1011.
The input value t represents the type of test case, 0 ≤ t < 4:
When t = 0 or 2, each of the input cities v will have fv = v − 1. That is, the cities will form a chain with SZ at one end.
When t = 0 or 1, each of the input cities v will have lv = 2×1011. That is, there will be no distance limit for traveling between cities – every city will be able to reach every one of its ancestors.
When t = 3, there will be no special characteristics for the test case.
The sizes of n and t for each test case are summarized as follows.
|1||n = 2×10||t = 2|
|2||n = 2×103||t = 0|
|3||t = 3|
|4||n = 2×105||t = 0|
|5||t = 2|
|6||t = 1|
|9||t = 3|
The input and output requires 64-bit integers. If your calculations require operating on two 64-bit integers, be careful of whether the result will overflow.
Point Value: 30 (partial)
Time Limit: 3.00s
Memory Limit: 512M
Added: May 20, 2015
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3