#P4297. [NOI2006] 网络收费
[NOI2006] 网络收费
Description
The network has become an indispensable part of today’s world. Hundreds of millions of people use the network every day for learning, research, entertainment, and more. However, it is important to note that the network itself has huge operating costs. Therefore, it is necessary and reasonable to charge users appropriately.
NS Middle School in MY City has such an educational network. There are users in the network, numbered . These users are connected by router nodes and network cables. The users, router nodes, and network cables together form a full binary tree. Every leaf node of the tree is a user, every non-leaf node (gray) is a router node, and every edge is a network cable (see the figure; the numbers in user nodes are their IDs).
MY Network Company uses an unusual pricing model called “paired charging.” That is, fees are charged for every pair of users with . Since each user can independently choose one of the two payment plans A or B, the fee the company charges the school depends on each user’s plan. The total fee equals the sum of the fees generated by all pairs of distinct users.
For convenience, we first define some concepts on this network tree:
- Ancestor: The root node has no ancestors. For a non-root node, its ancestors include its parent and all ancestors of its parent.
- Covered leaf nodes: A leaf node covers no leaf nodes. A non-leaf node covers the leaf nodes covered by its left child and the leaf nodes covered by its right child.
- Distance: The number of edges on a shortest path connecting two nodes in the tree.
For any two users with , first find their nearest common ancestor (LCA) in the tree: router node . Then, among the leaf nodes (i.e., users) covered by , count how many chose plan A and how many chose plan B, denoted and , respectively. Next, the fee is charged according to Article X, Section Y, Clause Z of the Network Management Regulations (see the table below), where is the traffic between and , and is known.
Since the final amount paid depends on the choice of payment plans, users at NS Middle School hope to change their plans to reduce the total payment. However, since the network company has recorded the plan each user chose at registration, user must pay yuan to the company to modify the record (switch plan A to B or B to A).
Now the problem: given each user’s initial plan and the values , determine how these users should choose their plans to minimize the total fee NS Middle School pays to the network company (switching fees + paired charging fees).
Input Format
The first line contains a positive integer .
The second line contains integers, giving the initial plan of users . If a number is 0, the corresponding user’s initial plan is A; otherwise the number is 1, meaning plan B.
The third line contains integers, the switching fee for each user, namely .
The next lines describe the traffic table between every pair of users. Specifically, in the overall line , the integer in column equals , for and .
See the problem statement for the meaning of all variables.
Output Format
Output a single integer: the minimum total fee (in yuan) that NS Middle School pays to the network company.
2
1 0 1 0
2 2 10 9
10 1 2
2 1
3
8
Hint
Sample Explanation: Changing user from plan B to plan A yields the minimum total payment.
Constraints:
- In of the testdata, .
- In of the testdata, .
- In of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号