#P1268. 树的重量
树的重量
Description
Trees can be used to represent evolutionary relationships among species. An "evolutionary tree" is a tree with edge weights, where each leaf node represents a species, and the distance between two leaves represents the difference between the two species. Now, an important problem is to reconstruct the corresponding "evolutionary tree" from the distances between species.
Let , and use a matrix on to define a tree . The matrix satisfies: for any , , , we have . The tree satisfies:
- The leaves are the elements of .
- All edge weights are non-negative integers.
- , where denotes the length of the shortest path between and in the tree.
As shown below, the matrix describes a tree.
$$M=\begin{bmatrix} 0 & 5 & 9 & 12 & 8 \\ 5 & 0 & 8 & 11 & 7 \\ 9 & 8 & 0 & 5 & 1 \\ 12 & 11 & 5 & 0 & 4 \\ 8 & 7 & 1 & 4 & 0 \\ \end{bmatrix}$$The weight of a tree is the sum of all edge weights in the tree. For any valid matrix , the total weight of the tree it represents is uniquely determined. It is impossible to find two trees with different total weights that are both consistent with . Your task is to compute the weight of the tree represented by the given matrix . The figure below shows one tree represented by the above matrix , and the total weight of this tree is .

Input Format
The first line contains an integer with .
Then lines follow, giving the strict upper triangular part of matrix (excluding the diagonal). All entries in the matrix are non-negative integers not exceeding . The input is guaranteed to be valid. Matrix is symmetric with zeros on the diagonal. On the -th of the next lines (), there are integers: .
Output Format
Output one line containing a single integer, the total weight of the tree.
5
5 9 12 8
8 11 7
5 1
4
15
4
15 36 60
31 55
36
71
Hint
Translated by ChatGPT 5
京公网安备 11011102002149号