A10885 | Send the Fool Further! (medium)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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?
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$ () 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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted