#abc214d. D - Sum of Maximum Weights

D - Sum of Maximum Weights

Score : 400400 points

问题描述

我们有一个包含 NN 个顶点的树,顶点编号为 1,2,,N1, 2, \dots, N
ii 条边(1iN11 \leq i \leq N - 1)连接顶点 uiu_i 和顶点 viv_i,并具有权重 wiw_i

对于不同的顶点 uuvv,令 f(u,v)f(u, v) 表示从顶点 uu 到顶点 vv 的最短路径中包含的最大权重边。

求 $\displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j)$。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a tree with NN vertices numbered 1,2,,N1, 2, \dots, N.
The ii-th edge (1iN1)(1 \leq i \leq N - 1) connects Vertex uiu_i and Vertex viv_i and has a weight wiw_i.

For different vertices uu and vv, let f(u,v)f(u, v) be the greatest weight of an edge contained in the shortest path from Vertex uu to Vertex vv.

Find $\displaystyle \sum_{i = 1}^{N - 1} \sum_{j = i + 1}^N f(i, j)$.

Constraints

  • 2N1052 \leq N \leq 10^5
  • 1ui,viN1 \leq u_i, v_i \leq N
  • 1wi1071 \leq w_i \leq 10^7
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

u1u_1 v1v_1 w1w_1

\vdots

uN1u_{N - 1} vN1v_{N - 1} wN1w_{N - 1}

Output

Print the answer.

Sample Input 1

3
1 2 10
2 3 20

Sample Output 1

50

We have f(1,2)=10f(1, 2) = 10, f(2,3)=20f(2, 3) = 20, and f(1,3)=20f(1, 3) = 20, so we should print their sum, or 5050.

Sample Input 2

5
1 2 1
2 3 2
4 2 5
3 5 14

Sample Output 2

76

update @ 2024/3/10 09:31:34