/* HDU 6060 - RXD and dividing [ 分析,图论 ] | 2017 Multi-University Training Contest 3 题意: 给一个 n 个节点的树,要求将 2-n 号节点分成 k 部分,然后将每一部分加上节点 1, 每一个子树的 val 为最小斯坦纳树,求总的最大 val 分析: 考虑每条边下面所在的子树,大小为num 由于该子树至多被分成 k 块,故该边最多贡献 k 次,贡献次数当然是越多越好 所以每条边的贡献为 w * min(k, num) */ #include <bits/stdc++.h> using namespace std; const int N = 1000005; #define LL long long struct Edge { int to, w, next; }edge[N<<1]; int head[N], tot; void init() { memset(head, -1, sizeof(head)); tot = 0; } void addedge(int u, int v, int w) { edge[tot].to = v; edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot++; } int sum[N], w[N]; int dfs(int u, int pre) { sum[u] = 1; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (v == pre) continue; sum[u] += dfs(v, u); w[v] = edge[i].w; } return sum[u]; } int k, n; int main() { while (~scanf("%d%d", &n, &k)) { init(); int a, b, c; for (int i = 1; i < n; i++) { scanf("%d%d%d", &a, &b, &c); addedge(a, b, c); addedge(b, a, c); } dfs(1, 1); LL ans = 0; for (int i = 2; i <= n; i++) { ans += (LL)min(sum[i], k) * w[i]; } printf("%lld ", ans); } }