CF1004E.Sonya and Ice Cream
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops.
Sonya lives in a city with n junctions and n−1 streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets.
Sonya has exactly k friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally.
Sonya wants all her ice cream shops to form a simple path of the length r (1≤r≤k), i.e. to be located in different junctions f1,f2,…,fr and there is street between fi and fi+1 for each i from 1 to r−1.
The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions a and b is equal to the sum of all the street lengths that you need to pass to get from the junction a to the junction b. So Sonya wants to minimize
max_amin_1leilerd_a,f_i
where a takes a value of all possible n junctions, fi — the junction where the i-th Sonya's shop is located, and dx,y — the distance between the junctions x and y.
Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than k shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal.
索尼娅非常喜欢冰淇淋,甚至在编程竞赛期间也会吃。因此,她决定开设自己的冰淇淋店。
索尼娅居住在一个拥有 n 个路口和 n−1 条连接它们的街道的城市中。所有街道都是双向的,且连接两个路口。任意两个路口之间均可通过一条或多条街道相互到达。市政厅只允许在路口处开设店铺,不允许在街道中间开设店铺。
索尼娅恰好有 k 位她可以信赖的朋友。如果她开设一家店铺,则必须由其中一位朋友在此工作,以阻止任何人不付钱就吃冰淇淋。由于索尼娅不想错过一场重要的竞赛,她本人不会亲自在店铺中工作。
索尼娅希望她的所有冰淇淋店铺构成一条长度为 r(1≤r≤k)的简单路径,即这些店铺位于互不相同的路口 f1,f2,…,fr 上,且对每个从 1 到 r−1 的 i,路口 fi 与 fi+1 之间都有一条街道相连。
索尼娅还关心潜在顾客的便利性,因此她希望最小化所有路口到最近冰淇淋店的最大距离。两个路口 a 和 b 之间的距离定义为从 a 到 b 所需经过的所有街道长度之和。因此,索尼娅希望最小化:
amax1≤i≤rminda,fi
其中 a 取遍全部 n 个路口,fi 表示第 i 家索尼娅店铺所在的路口,dx,y 表示路口 x 与 y 之间的距离。
索尼娅不确定自己能否找到最优的店铺位置,因此她请你帮忙:至多开设 k 家店铺,使其构成一条简单路径,并使得任意路口到最近店铺的最大距离尽可能小。
输入格式
The first line contains two integers n and k (1≤k≤n≤105) — the number of junctions and friends respectively.
Each of the next n−1 lines contains three integers ui, vi, and di (1≤ui,vi≤n, vi=ui, 1≤d≤104) — junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other.
第一行包含两个整数 n 和 k(1≤k≤n≤105),分别表示路口的数量和朋友的数量。
接下来的 n−1 行每行包含三个整数 ui、vi 和 di(1≤ui,vi≤n,vi=ui,1≤d≤104),表示由一条街道连接的两个路口以及该街道的长度。保证任意两个路口之间至多只有一条街道相连。保证从任意一个路口均可到达其他任意路口。
输出格式
Print one number — the minimal possible maximum distance that you need to pass to get from any junction to the nearest ice cream shop. Sonya's shops must form a simple path and the number of shops must be at most k.
输出一个数字——从任意路口到最近的冰淇淋店所需经过的最小可能的最大距离。索尼娅的店铺必须构成一条简单路径,且店铺数量至多为 k。
输入输出样例
输入#1
6 2 1 2 3 2 3 4 4 5 2 4 6 3 2 4 6
输出#1
4
输入#2
10 3 1 2 5 5 7 2 3 2 6 10 6 3 3 8 1 6 4 2 4 1 6 6 9 4 5 2 5
输出#2
7
说明/提示
In the first example, you can choose the path 2-4, so the answer will be 4.
The first example.
In the second example, you can choose the path 4-1-2, so the answer will be 7.
The second example.
在第一个例子中,你可以选择路径 2-4,因此答案为 4。
第一个例子。
在第二个例子中,你可以选择路径 4-1-2,因此答案为 7。
第二个例子。
输入解题思路,AI测评打分。不知道怎么写?