tickets_loj

Buying Tickets

This summer, NOI celebrates her 30th birthday in the city of SZ. OIers will travel in from cities all across the country to attend this event in SZ.

The cities of the country form a rooted tree with the city of SZ as its root, and each city is connected to its parent by road. For convenience, we number the n cities with integers from 1 to n. SZ will be city 1, and every other city v is connected to its parent fv by a road of length sv.

To travel from city v to city SZ, one can choose an ancestor a of city v, pay for the ticket, and take the transit to a. Then, choose an ancestor b of city a, pay for the ticket, arrive at b, and so on until you arrive at city SZ.

Each city v also has a distance limit lv for its transit system. For each ancestor a of city v, city a can be reached directly from city v if and only if the total length of all roads between them does not exceed lv. Each city v also has two non-negative integers pv, qv as fare parameters. If the total length of roads in the path from city v to city a is d, then the ticket cost of going directly from city v to city a is pvd+qv.

The OIer in each city wants to arrive in SZ while minimising their spending on tickets. Your task is to tell the OIers in each city what is the minimum amount of money they have to spend.

Limits

2n2105, 0pv106, 0qv1012, 1fv<v, 0<svlv21011.
The distance from any one city to city SZ will not exceed 21011.
Input t represents the data type. 0t3, where:
  • When t=0 or t=2, all fv=v1, that is, the cities form a line rooted at SZ.
  • When t=0 or t=1, all lv=21011, that is, there is no distance limit imposed on the cities.
  • When t=3, none of the above constraints apply.

Subtask # Score Constraints
1 10 n20
2 20 n2000
3 10 t=0
4 30 t=1
5 10 t=2
6 20 No additional constraints

Input format

The first line of input contains two integers n, t, indicating the number of cities and the data type respectively. (The Data Type is explained under Limits.)

The next n1 lines of input respectively describe cities 2 through n. The line describing city v contain 5 non-negative integers fv,sv,pv,qv,lv, respectively representing the parent of city v, the length of the road from v to its parent, the two fare parameters of v, and the transit distance limit from v.

Output format

Output n1 lines each containing a single integer. Line v must contain the minimum cost of tickets to get from city v+1 to SZ.

Samples

Sample Input 1 Sample Output 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
40
150
70
149
300
150


Submitting .cpp to 'tickets_loj'


You're not logged in! Click here to login

Time Limit: 3 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: LOJ #2249

Subtask Score
1 10
2 20
3 10
4 30
5 10
6 20
7 0