#P3354. [IOI 2005] Riv 河流
[IOI 2005] Riv 河流
Description
Almost the entire kingdom of Byteland is covered with forests and rivers. Small rivers merge into slightly larger ones. In this way, all river water converges into a single large river, which eventually flows into the sea. At the mouth of this large river lies a village called Bytetown.
In Byteland, there are logging villages located along the rivers. There is currently a large sawmill in Bytetown that processes all the timber cut nationwide. Timber is cut and transported downstream along the rivers to the sawmill in Bytetown. The King of Byteland has decided to reduce transportation costs by building additional sawmills in other villages. After these sawmills are built, timber no longer needs to be sent all the way to Bytetown; it can be processed at the first new sawmill encountered during transport. Clearly, if a sawmill is located in a village, that village pays no transportation cost for its own timber; it can be processed locally.
Note: The rivers never split; they form a tree whose root is Bytetown.
The ministers have calculated the annual timber production of each village. Your task is to decide in which villages to build the sawmills to minimize the transportation cost. The cost is calculated as 1 cent per kilometer per ton of timber.
Input Format
The first line contains two integers . Here is the number of villages, and is the number of sawmills to build. All villages other than Bytetown are named , and Bytetown is named .
Lines through each contain three integers. On the -th line, the integers are:
- : the amount of timber produced annually by village (in tons),
- : the nearest downstream village from (i.e., the parent of village ),
- : the distance from to (in kilometers).
Output Format
Output the minimum total cost, in cents.
4 2
1 0 1
1 1 10
10 2 5
1 2 3
4
Hint
- Constraints:
- For of the testdata, .
- For of the testdata, , , , , .
- It is guaranteed that the annual cost of sending all timber to Bytetown does not exceed cents.
Translated by ChatGPT 5
京公网安备 11011102002149号