zoukankan      html  css  js  c++  java
  • 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 n1n−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 (1rk1≤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 r1r−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

    maxamin1irda,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 (1kn1051≤k≤n≤105) — the number of junctions and friends respectively.

    Each of the next n1n−1 lines contains three integers uiui, vivi, and didi (1ui,vin1≤ui,vi≤n, viuivi≠ui, 1d1041≤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 2
    1 2 3
    2 3 4
    4 5 2
    4 6 3
    2 4 6
    output
    Copy
    4
    input
    Copy
    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
    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
    ", ans);
    27     return 0;
    28 }
  • 相关阅读:
    第1章 游戏之乐——NIM(3)两堆石头的游戏
    NFS介绍和安装
    shell脚本删除N天前的目录-----附linux和mac上date命令的不同
    关于子线程更新UI
    WIN7下安装SVNserver端及client搭建协作环境
    Selenium_WebDriver登录模拟鼠标移动切换窗体等操作练习(cssSelector初练手)_Java
    UICollectionView具体解释
    局域网使用的IP地址范围
    hdu 2955 Robberies
    POJ 1661 Help Jimmy(DP,注意边界)
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9315722.html
Copyright © 2011-2022 走看看