#P3994. 高速公路

    ID: 7582 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>单调队列O2优化斜率优化队列

高速公路

Description

Country C has a well-connected highway network that forms a tree.

There are nn cities in Country C, connected by a total of n1n - 1 highways. Except for the capital, city 11, each city has a local passenger transport company that can dispatch vehicles to various places nationwide. You can regard the network as a tree rooted at 11. The distance between two cities is defined as the length of the simple path between them.

Suppose a person departs from city ii to city jj, which is at distance DD from ii. Then the cost is Pi×D+QiP_i \times D + Q_i yuan, and it is required that jj is an ancestor of ii. Because national supervision becomes looser farther from the capital, the PiP_i of transport companies increases with distance from the capital: if city ii is an ancestor of city jj, then PiPjP_i \leq P_j.

Xiao T has become an investigator at the national statistics bureau. He needs to survey the current highway network to find out the cost to reach the capital, city 11, from every other city.

Because reaching the capital may involve multiple transfers (or none), computing this by hand is very complicated. Xiao T is very lazy, so please write a program to solve it.

Input Format

The first line contains an integer nn, the number of cities.

Lines 22 through nn each describe one city other than the capital. Specifically, line ii contains four integers Fi,Si,Pi,QiF_i, S_i, P_i, Q_i, representing the parent city of city ii, the length of the highway from city ii to its parent, and the two fare parameters of city ii.

Output Format

Output n1n - 1 lines, each containing an integer.

The ii-th line contains the minimum cost to reach the capital starting from city i+1i + 1.

6
1 9 3 0
1 17 1 9
1 1 1 6
4 13 2 15
4 9 2 4

27
26
7
43
24

Hint

Constraints and Notes:

  • For the first 40%40\% of the testdata, n1000n \leq 1000.
  • For another 20%20\% of the testdata, Fi=i1F_i = i - 1.
  • For all the testdata, 1n1061 \leq n \leq 10^6, 0Pi,Qi<2310 \leq P_i, Q_i < 2^{31}, and the result is guaranteed not to exceed 26312^{63} - 1.

Translated by ChatGPT 5