#P2305. [NOI2014] 购票

    ID: 1265 远端评测题 3000ms 512MiB 尝试: 1 已通过: 1 难度: 9 上传者: 标签>图论2014线段树NOI 系列分治斜率优化

[NOI2014] 购票

Description

This summer, NOI celebrated its 30th anniversary in SZ City. OIers from nn cities across the country will depart from their homes to attend the event in SZ City.

All cities form a rooted tree with SZ City as the root, and each city is connected to its parent by a road. For convenience, we number the nn cities with integers from 11 to nn, where the number of SZ City is 11. For any city vv other than SZ City, we are given its parent city fvf_v in this tree and the length svs_v of the road to its parent.

To travel from city vv to SZ City, the method is as follows: choose an ancestor aa of city vv, pay for a ticket, and take a vehicle to aa. Then choose an ancestor bb of city aa, pay, and reach bb. Repeat this process until you arrive at SZ City.

For any city vv, we are given a distance limit lvl_v for its transportation. For an ancestor AA of city vv, city vv can reach AA with a single ticket if and only if the total length of all roads along the path between them does not exceed lvl_v; otherwise, it cannot reach AA with a single ticket.

For each city vv, we are also given two non-negative integers pv,qvp_v, q_v as fare parameters. If the total length of all roads from city vv to city AA is dd, then the price of the ticket from city vv to city AA is dpv+qvd p_v + q_v.

OIers in each city want to minimize the total amount of money spent on tickets when traveling to SZ City. Your task is to tell the OIers in each city their minimum total cost.

Input Format

The first line contains two non-negative integers n,tn, t, denoting the number of cities and the testdata type (its meaning is mentioned in “Hint”).

The next lines 2n2 \sim n each describe a city other than SZ City. Specifically, line vv contains five non-negative integers fv,sv,pv,qv,lvf_v, s_v, p_v, q_v, l_v, denoting the parent city of city vv, the length of the road to its parent, the two fare parameters, and the distance limit, respectively.

Note: The input does not include SZ City with number 11. Lines 2n2 \sim n describe cities 22 to nn.

Output Format

The output contains n1n - 1 lines, each with an integer.

The vv-th line gives the minimum total ticket cost to reach SZ City starting from city v+1v + 1.

Likewise, note that SZ City with number 11 is not included in the output.

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

Hint

The routes from each city to SZ City are as follows (an arrow denotes a single direct trip):

City 22: Can only choose 212 \rightarrow 1, with cost 2×20+0=402 \times 20 + 0 = 40.

City 33: Can only choose 313 \rightarrow 1, with cost 5×10+100=1505 \times 10 + 100 = 150.

City 44: Since 4+2=6l4=104 + 2 = 6 \leq l_4 = 10, it can choose 414 \rightarrow 1. If choosing 414 \rightarrow 1, the cost is (4+2)×10+10=70(4 + 2) \times 10 + 10 = 70; if choosing 4214 \rightarrow 2 \rightarrow 1, the cost is (4×10+10)+(2×20+0)=90(4 \times 10 + 10) + (2 \times 20 + 0) = 90. Therefore, choose 414 \rightarrow 1.

City 55: Can only choose 5215 \rightarrow 2 \rightarrow 1, with cost (9×1+100)+(2×20+0)=149(9 \times 1 + 100) + (2 \times 20 + 0) = 149. It cannot choose 515 \rightarrow 1 because l5=10l_5 = 10, while the total distance from city 55 to city 11 is 9+2=11>109 + 2 = 11 \gt 10, so city 55 cannot reach city 11 directly.

City 66: If choosing 616 \rightarrow 1, the cost is (5+5)×20+100=300(5 + 5) \times 20 + 100 = 300; if choosing 6316 \rightarrow 3 \rightarrow 1, the cost is (5×20+100)+(5×10+100)=350(5 \times 20 + 100) + (5 \times 10 + 100) = 350. Therefore, choose 616 \rightarrow 1.

City 77: Choose 7417 \rightarrow 4 \rightarrow 1, with cost (4×20+0)+((4+2)×10+10)=150(4 \times 20 + 0) + ((4 + 2) \times 10 + 10) = 150.

All other options are worse than this plan.

Constraints

For all testdata, n2×105n \leq 2 \times 10^5, 0pv1060 \leq p_v \leq 10^6, 0qv10120 \leq q_v \leq 10^{12}, 1fv<v1 \leq f_v < v, 0<svlv2×10110 < s_v \leq l_v \leq 2 \times 10^{11}, and the total path length from any city to SZ City does not exceed 2×10112 \times 10^{11}.

The input tt denotes the testdata type, 0t<40 \leq t < 4, where:

  • When t=0t = 0 or 22, for all cities vv in the input, fv=v1f_v = v - 1, i.e., all cities form a chain ending at SZ City.
  • When t=0t = 0 or 11, for all cities vv in the input, lv=2×1011l_v = 2 \times 10^{11}, i.e., there is no distance limit, and every city can reach all of its ancestors.
  • When t=3t = 3, the testdata has no special property.

Translated by ChatGPT 5