[NOI2014]Ticketing---Slope Optimization + Tree DP + Data Structure

Topic description

This summer, NOI celebrated her 30th birthday in SZ City.

OIers from n cities across the country will travel from all over the country to SZ City to participate in this event.

The cities of the country form a rooted tree with the SZ city as its root, and each city is connected with its father by roads.

For convenience, we number the n cities in the country with integers from 1 to n. The number of SZ city is 1.

For any city v except SZ city, we give its parent city fv on this tree and the length sv of the road to the parent city.

The way to go to SZ city from city v is: select an ancestor a of city v, pay the cost of purchasing tickets, and take transportation to reach a.

Pick another ancestor b in city a, pay the fee and get to b. And so on, until reaching SZ city.

For any city v, we will give a distance limit lv of a vehicle.

For ancestor a of city v, only if the total length of all roads between them does not exceed lv, can city a be reached by one ticket purchase from city v, otherwise it cannot be reached by one ticket purchase.

For each city v, we also give two non-negative integers pv,qv as fare parameters.

If the total length of all roads from city v to city a is d, then the fare purchased from city v to city a is d * pv+qv.

The OIer in each city wants to get to SZ City by themselves with the least amount of money spent on ticket purchases.

Your task is to tell the OIer in each city what the minimum amount of money they will spend.

 

Input and output format

Input format:

Line 1 contains 2 non-negative integers n, t, representing the number and data type of the city respectively (the meaning of which will be mentioned later).

Lines 2 to n of the input file, each describing a city other than SZ.

The vth row contains 5 non-negative integers f_v, s_v, p_v, q_v, l_v, which respectively represent the father city of city v, the length of the road from it to the father city, the two parameters of the fare and the distance limit.

Please note: the input does not contain the SZ city numbered 1, rows 2 to n describe city 2 to city n, respectively.

 

Output format:

The output contains n-1 lines, each containing an integer.

Among them, the vth row represents the minimum ticket purchase cost from the city v+1 to the city of SZ.

Also note: the output does not contain the SZ city numbered 1.

 

Input and output example

Input example #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
Sample output #1:
40
150
70
149
300
150

illustrate

The route from each city to SZ is as follows (where the arrows indicate a direct route):

City 2: Only 2 → 1 can be selected, and the cost is 2 × 20 + 0 = 40.

City 3: Only 3 → 1 can be selected, and the cost is 5 × 10 + 100 = 150. City 4: Since 4 + 2 =6 ≤ l4 = 10, you can choose 4→1.

If you choose 4 → 1, the cost is (4 + 2) × 10 + 10 = 70; if you choose 4 → 2 → 1, the cost is (4 × 10 + 10) + (2 × 20 + 0) = 90; therefore Choose 4→1.

City 5: Only 5 → 2 → 1 can be selected, and the cost is (9 × 1 +100) + (2 × 20 + 0) = 149; 5 → 1 cannot be selected, because l5 = 10, and the total cost of city 5 to city 1 The distance is 9 + 2 = 11 > 5, city 5 cannot reach city 1 directly.

City 6: If you choose 6 → 1, the cost is (5 + 5) × 20 + 100 = 300; if you choose 6 → 3 → 1, the cost is (5 × 20 + 100) + (5 × 10 + 100) = 350 ; so choose 6 → 1.

City 7: choose 7 → 4 → 1, the cost is (4 × 20 + 0) + ((4 + 2) × 10 + 10) = 150;

All other programs are worse than this program.

data scale

 

This way it seems to be a bit faster. . . .

Slope optimization tree DP, it is easier to see

How to maintain distance restrictions?

(More details will be written later)

1. Tree chain segmentation

2. Persistence (chairman tree + monotonic queue)

3. Persistence (chairman tree + balance tree)

4. Point divide and conquer + CDQ divide and conquer

5. Divide and conquer + sort (my way)

6. Divide and conquer + persistent balanced tree

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324930257&siteId=291194637