#P3994. 高速公路
高速公路
Description
Country C has a well-connected highway network that forms a tree.
There are cities in Country C, connected by a total of highways. Except for the capital, city , 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 . The distance between two cities is defined as the length of the simple path between them.
Suppose a person departs from city to city , which is at distance from . Then the cost is yuan, and it is required that is an ancestor of . Because national supervision becomes looser farther from the capital, the of transport companies increases with distance from the capital: if city is an ancestor of city , then .
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 , 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 , the number of cities.
Lines through each describe one city other than the capital. Specifically, line contains four integers , representing the parent city of city , the length of the highway from city to its parent, and the two fare parameters of city .
Output Format
Output lines, each containing an integer.
The -th line contains the minimum cost to reach the capital starting from city .
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 of the testdata, .
- For another of the testdata, .
- For all the testdata, , , and the result is guaranteed not to exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号