#P3345. [ZJOI2015] 幻想乡战略游戏
[ZJOI2015] 幻想乡战略游戏
Description
The tsundere girl Yuuka is playing a very interesting strategy game. At first, the game map was not too large, and Yuuka could still manage it. But for some reason, the online game developers are making the map larger and larger, to the point that Yuuka can no longer take it all in at a glance, let alone fight others.
Before going to war, Yuuka needs to solve a very basic management problem.
The entire map is a tree with empty fields (nodes), connected by weighted edges, so that there is a unique path between any two nodes.
In the game, Yuuka may increase or decrease troops on empty fields. At the same time, she can place one supply station on a node. If the supply station is at node , and there are units of troops at node , then Yuuka must spend money per day to supply these troops. Since Yuuka needs to supply all troops, the total cost is (where ). Here, denotes the distance between and on the tree (the sum of weights along the unique path).
Due to the game rules, Yuuka can choose only one node as the supply station. During the game, she may create troops on some nodes or reduce troops on some nodes. After such operations, for economic reasons, she may move the supply station to save money. However, since the map is just too large, Yuuka cannot easily make the optimal arrangement. Can you help her?
You may assume that initially all nodes have no troops.
Input Format
The first line contains two integers and , denoting the number of nodes in the tree and the number of Yuuka’s operations, respectively. Nodes are labeled from to .
The next lines each contain three positive integers , indicating that there is an edge between and with weight .
The next lines each contain two integers , meaning Yuuka adds units of troops at node (if , that means Yuuka removes units at ; in other words, ).
It is guaranteed that at any time, the number of troops at each node is non-negative.
Output Format
For each of Yuuka’s operations, output the minimal daily cost after the operation, i.e., the cost if Yuuka chooses the optimal supply station.
10 5
1 2 1
2 3 1
2 4 1
1 5 1
2 6 1
2 7 1
5 8 1
7 9 1
1 10 1
3 1
2 1
8 1
3 1
4 1
0
1
4
5
6
Hint
For all testdata, , , , .
Interestingly, for all testdata, the degree of each node in the tree does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号