E. Sonya and Ice Cream(开拓思维)

E. Sonya and Ice Cream

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nn junctions and n?1n?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 kk 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 rr (1≤r≤k1≤r≤k), i.e. to be located in different junctions f1,f2,…,frf1,f2,…,fr and there is street between fifi and fi+1fi+1 for each ii from 11 to r?1r?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 aa and bb is equal to the sum of all the street lengths that you need to pass to get from the junction aa to the junction bb. So Sonya wants to minimize

maxamin1≤i≤rda,fimaxamin1≤i≤rda,fi

where aa takes a value of all possible nn junctions, fifi — the junction where the ii-th Sonya‘s shop is located, and dx,ydx,y — the distance between the junctions xx and yy.

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 kk shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal.

Input

The first line contains two integers nn and kk (1≤k≤n≤1051≤k≤n≤105) — the number of junctions and friends respectively.

Each of the next n?1n?1 lines contains three integers uiui, vivi, and didi (1≤ui,vi≤n1≤ui,vi≤n, vi≠uivi≠ui, 1≤d≤1041≤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.

Output

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 kk.

Examples

input

Copy

6 21 2 32 3 44 5 24 6 32 4 6

output

Copy

4

input

Copy

10 31 2 55 7 23 2 610 6 33 8 16 4 24 1 66 9 45 2 5

output

Copy

7

Note

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.

求一个树的直径

  这个运用了数据结构set优化  太强了 大佬操作

  一定要好好学学这个操作  

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 10;
 4 set<pair<int, int> > d[maxn], s;
 5 int n, k, ans = 0;
 6 int main() {
 7     scanf("%d%d", &n, &k);
 8     for (int i = 0 ; i < n - 1 ; i++ ) {
 9         int u, v, w;
10         scanf("%d%d%d", &u, &v, &w);
11         d[u].insert(make_pair(v, w));
12         d[v].insert(make_pair(u, w));
13     }
14     for (int i = 1 ; i <= n ; i++)
15         if (d[i].size() == 1) s.insert(make_pair((*d[i].begin()).second, i));
16     while( n > k || s.size() > 2) {
17         ans = (*s.begin()).first;
18         int i = (*s.begin()).second;
19         s.erase(s.begin());
20         int next = (*d[i].begin()).first;
21         d[next].erase(d[next].lower_bound(make_pair(i, 0)));
22         n--;
23         if (d[next].size() == 1)
24             s.insert(make_pair((*d[next].begin()).second + ans, next));
25     }
26     printf("%d\n", ans);
27     return 0;
28 }

原文地址:https://www.cnblogs.com/qldabiaoge/p/9315722.html

时间: 2024-10-01 05:27:23

E. Sonya and Ice Cream(开拓思维)的相关文章

Ice Cream Tower

2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output file: Standard Ouptut Time limit: 6 seconds Mr. Panda likes ice cream very much especially the ice cream tower. An ice cream tower consists of K ice cr

HackerRank Ice Cream Parlor

传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together have M dollars they want to spend on ice cream. The parlor offers N flavors, and they want to choose two flavors so that they end up spending the whol

codeforces 686A A. Free Ice Cream(水题)

题目链接: A. Free Ice Cream //#include <bits/stdc++.h> #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using

【HackerRank】Ice Cream Parlor

Sunny and Johnny together have M dollars which they intend to use at the ice cream parlour. Among N flavors available, they have to choose two distinct flavors whose cost equals M. Given a list of cost of N flavors, output the indices of two items wh

How to Implement Bluetooth Low Energy (BLE) in Ice Cream Sandwich

ShareThis - By Vikas Verma Bluetooth low energy (BLE) is a feature of Bluetooth 4.0 wireless radio technology, aimed at new, principally low-power and low-latency, applications for wireless devices within a short range. As I discussed in my previous

Gym - 101670G Ice cream samples(CTU Open Contest 2017 尺取法)

题目: To encourage visitors active movement among the attractions, a circular path with ice cream stands was built in the park some time ago. A discount system common for all stands was also introduced. When a customer buys ice cream at some stand, he

CodeForces 804C Ice cream coloring

Ice cream coloring 题解: 这个题目中最关键的一句话是, 把任意一种类型的冰激凌所在的所有节点拿下来之后,这些节点是一个连通图(树). 所以就不会存在多个set+起来之后是一个新的完全图. 所以只要直接去做就好了. 对于每个节点来说,染色. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freope

CodeForces 686A Free Ice Cream (水题模拟)

题意:给定初始数量的冰激凌,然后n个操作,如果是“+”,那么数量就会增加,如果是“-”,如果现有的数量大于等于要减的数量,那么就减掉,如果小于, 那么孩子就会离家.问你最后剩下多少冰激凌,和出走的孩子数量. 析:多水的一个题,就是一个模拟,如果是+,就加上,如果是‘-’,就判断一下,如果不够,就记录下来. 代码如下: #include <iostream> #include <cmath> #include <cstdlib> #include <set>

The 2016 ACM-ICPC Asia China-Final Contest Gym - 101194D Ice Cream Tower

题意:熊猫先生想用一些冰淇淋球来做k层的冰淇淋,规定对于一个冰淇淋中的每一个冰淇淋球(最顶层除外),都大于等于上一层的冰淇淋球的两倍大小:现有n个冰淇淋球,问最多能做几个冰淇淋 思路:刚开始想的贪心,最后发现是不行的.比如对于 1 2 3 4 这种情况,1 后面是2还是3就没法用程序来进行抉择了.可以用二分的方法,枚举所有可能的冰淇淋个数x,然后再进行判断当前的冰淇淋球能否做成x个冰淇淋 1 #include <bits/stdc++.h> 2 using namespace std; 3 t