#P3354. Byteland Lumber Transportation Optimization
Byteland Lumber Transportation Optimization
Byteland Lumber Transportation Optimization
In the kingdom of Byteland, almost the entire territory is covered by forests and rivers. Small streams merge into larger rivers until finally all water flows into one huge river that empties into the sea. At the river’s mouth lies the village of Bytetown.
There are logging villages located along the river, and each village produces a certain amount of timber per year. Bytetown already has a huge sawmill that processes all timber floated down the river. To reduce transportation costs, the king has decided to build additional sawmills in some of the other villages. When timber is carried downstream, it is processed at the first sawmill encountered. Thus, if a village has its own sawmill then its timber need not be transported farther, saving transportation cost.
All rivers do not bifurcate and form a tree with Bytetown as the root (which already has a sawmill). The cost for transporting timber is computed as follows: for every ton of timber, it costs 1 cent per kilometer.
Assume that if no additional sawmill is built on the path from a village to Bytetown, then the cost incurred for that village is the total distance (summed over all edges on its unique path) multiplied by its timber production. When a sawmill is built at a village, the cost for the entire subtree (i.e. that village and all villages downstream from it) is saved for the edge connecting that village to its parent because the timber is processed locally.
We can model the problem as follows. Let the input consist of an integer (the number of villages) and an integer (the count of additional sawmills to build). For the villages, the input format is defined as below:
For village 1 (Bytetown), a single integer is provided, indicating its timber production. For every other village (), three integers are given: the index of its parent village, the distance (in kilometers) from village to its parent, and its timber production .
Let denote the total timber production in the subtree rooted at village . The baseline cost (with no additional sawmills built) is given by [ \text{Baseline Cost} = \sum_{v=2}^{n} S(v) \times d_v. ]
Building a sawmill at a non-root village saves (S(v) \times d_v) cents. The objective is to choose at most villages (other than village 1) in which to build sawmills so that the overall transportation cost is minimized.
Your task is to compute the minimum total transportation cost (in cents) after optimally building additional sawmills.
inputFormat
The input is given as follows:
The first line contains two integers and , where is the number of villages and is the number of additional sawmills to build.
The next line contains a single integer representing the timber production of village 1 (Bytetown).
Then, for villages to , each line contains three integers: the index of the parent village, the distance (in kilometers) from this village to its parent, and the timber production of this village.
It is guaranteed that the villages form a tree with village 1 as the root.
outputFormat
Output a single integer: the minimum total transportation cost (in cents) after optimally building the additional sawmills.
sample
3 1
10
1 5 20
1 3 30
90