304198: CF802K. Send the Fool Further! (medium)

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Send the Fool Further! (medium)

题意翻译

## 题面 给定一棵节点数为 $n$ (编号为 $0-(n-1)$ )的树,每一条边有一个权值。现在要求从点0出发,在不经过一个点超过 $k$ 次的情况下经过的边的权值和最大。(每一条边在第一次经过之后权值即变为0;从点0出发时也视作经过了1次点0;最后不需要回到点0) ## 输入输出格式 ### 输入格式 输入的第一行包括两个正整数,节点数 $n$ 和每个点的最大访问次数 $k$ 。接下来的 $n-1$ 行每行有3个正整数 $u$ , $v$ 和 $c$ ,表示在顶点 $u$ 和顶点 $c$ 之间有一条权值为 $c$ 的无向边。 ### 输出格式 输出一个正整数——可以获得的最大权值。 ## 输入输出样例 略 ## 样例解释 对于样例1,访问各节点的顺序为: $0,1,5,1,3,1,0,2,6,2,7,2,8$ 。没有点被访问了超过3次。 ## 数据范围 $ 3≤n≤10^5 $ $ 1≤k≤10^5 $ $ 0≤u,v≤n-1, 1≤c≤10^4 $ 感谢@星烁晶熠辉 提供的翻译

题目描述

Thank you for helping Heidi! It is now the second of April, but she has been summoned by Jenny again. The pranks do not seem to end... In the meantime, Heidi has decided that she does not trust her friends anymore. Not too much, anyway. Her relative lack of trust is manifested as follows: whereas previously she would not be made to visit the same person twice, now she can only be sure that she will not be made to visit the same person more than $ k $ times. (In the case of Jenny, this includes her first visit in the beginning. The situation from the easy version corresponds to setting $ k=1 $ .) This is not as bad as it looks, since a single ticket for a route between two friends allows Heidi to travel between this pair of friends the whole day (in both directions). In other words, once she pays for travel between a pair of friends, all further travels between that pair are free. How much money will Heidi waste now, in a worst-case scenario?

输入输出格式

输入格式


The first line contains two space-separated integers – the number of friends $ n $ (![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF802K/61a469a4155af38f62f367d5c4fb45b1ec2c115c.png)) and the parameter $ k $ ( $ 1<=k<=10^{5} $ ). The next $ n-1 $ lines each contain three space-separated integers $ u $ , $ v $ and $ c $ ( $ 0<=u,v<=n-1 $ , $ 1<=c<=10^{4} $ ) meaning that $ u $ and $ v $ are friends and the cost for traveling between $ u $ and $ v $ is $ c $ . It is again guaranteed that the social network of the input forms a tree.

输出格式


Again, output a single integer – the maximum sum of costs of tickets.

输入输出样例

输入样例 #1

9 3
0 1 1
0 2 1
1 3 2
1 4 2
1 5 2
2 6 3
2 7 3
2 8 3

输出样例 #1

15

输入样例 #2

9 5
0 1 1
0 2 1
1 3 2
1 4 2
1 5 2
2 6 3
2 7 3
2 8 3

输出样例 #2

17

输入样例 #3

11 6
1 0 7932
2 1 1952
3 2 2227
4 0 9112
5 4 6067
6 0 6786
7 6 3883
8 4 7137
9 1 2796
10 5 6200

输出样例 #3

54092

说明

In the first example, the worst-case scenario for Heidi is to visit the friends in the following order: $ 0,1,5,1,3,1,0,2,6,2,7,2,8 $ . Observe that no friend is visited more than $ 3 $ times.

Input

题意翻译

## 题面 给定一棵节点数为 $n$ (编号为 $0-(n-1)$ )的树,每一条边有一个权值。现在要求从点0出发,在不经过一个点超过 $k$ 次的情况下经过的边的权值和最大。(每一条边在第一次经过之后权值即变为0;从点0出发时也视作经过了1次点0;最后不需要回到点0) ## 输入输出格式 ### 输入格式 输入的第一行包括两个正整数,节点数 $n$ 和每个点的最大访问次数 $k$ 。接下来的 $n-1$ 行每行有3个正整数 $u$ , $v$ 和 $c$ ,表示在顶点 $u$ 和顶点 $c$ 之间有一条权值为 $c$ 的无向边。 ### 输出格式 输出一个正整数——可以获得的最大权值。 ## 输入输出样例 略 ## 样例解释 对于样例1,访问各节点的顺序为: $0,1,5,1,3,1,0,2,6,2,7,2,8$ 。没有点被访问了超过3次。 ## 数据范围 $ 3≤n≤10^5 $ $ 1≤k≤10^5 $ $ 0≤u,v≤n-1, 1≤c≤10^4 $ 感谢@星烁晶熠辉 提供的翻译

加入题单

上一题 下一题 算法标签: