ccf#NOI2014F. 购票 (Buying Tickets)
购票 (Buying Tickets)
Description
This Summer, NOI is celebrating its 30th birthday in the city of SZ(Shenzhen). OIers attending this ceremony are travelling to SZ from cities around the country.
The cities in the country form a rooted tree with SZ as its root, and each city is connected to its parent by a road. For simplicity, we label the cities with integers from to . SZ city has a label of . For any city other than SZ labelled , its parent on the tree and length of the road to its parent is given.
To arrive in SZ from city , we first choose one of 's ancestors, , and pay for a ticket to . Then we choose 's ancestor and pay to arrive in . Repeat this process until SZ is reached.
For each city , a limit on travel distance is given. For as an ancestor of , a direct transportation from city to city could be taken only if the sum of roads' distances between them is not greater than ; otherwise it cannot be reached within one travel. For each city , parameters of transportation price are also known. if the total distance of roads from city to city is , then the price of ticket from city to city is .
OIers from every city want to spend less money to get to SZ. Your task is to tell them the minimum cost for OIers in each city.
Input Format
The first line contains two integers , indicating the number of cities and a brief description of input data (meaning explained later).
Each line from line to line describes a city other than SZ. Line consists of five non-negative integers , indicating the parent of city , the distance between them, the two parameters for ticket price, and the limitation on travel distance respectively.
Note: Input does not contain city , SZ. line to line describes city to city .
Output Format
The output consists of lines, each with one integer. Line is the minimum cost to travel from city to SZ.
Also Note: Output does not contain city , SZ.
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
Constraints and Hint
For all the data, , , , , , and the total distance from any city to SZ is not greater than .
in input describes input type, :
When or , for every city in input, , therefore the cities form a chain with SZ on one end;
When or , for every city in input, , therefore there's no limit on travel distance, all cities could travel directly to any of their ancestors;
When , no special case is expected.