E. Sonya and Ice Cream(开拓思维)
2 seconds
256 megabytes
standard input
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.
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.
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.
6 2
1 2 3
2 3 4
4 5 2
4 6 3
2 4 6
4
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
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.
求一个树的直径
这个运用了数据结构set优化 太强了 大佬操作
一定要好好学学这个操作
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
set<pair<int, int> > d[maxn], s;
int n, k, ans = ;
int main() {
scanf("%d%d", &n, &k);
for (int i = ; i < n - ; i++ ) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
d[u].insert(make_pair(v, w));
d[v].insert(make_pair(u, w));
}
for (int i = ; i <= n ; i++)
if (d[i].size() == ) s.insert(make_pair((*d[i].begin()).second, i));
while( n > k || s.size() > ) {
ans = (*s.begin()).first;
int i = (*s.begin()).second;
s.erase(s.begin());
int next = (*d[i].begin()).first;
d[next].erase(d[next].lower_bound(make_pair(i, )));
n--;
if (d[next].size() == )
s.insert(make_pair((*d[next].begin()).second + ans, next));
}
printf("%d\n", ans);
return ;
}
E. Sonya and Ice Cream(开拓思维)的更多相关文章
- Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心
题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...
- CodeForces - 1004E Sonya and Ice Cream
题面在这里! 挺智障的一个二分...我还写了好久QWQ,退役算啦 题解见注释... /* 先对每个点记录 向子树外的最长路 和 向子树内最长路,然后二分. 二分的时候枚举链的LCA直接做就好啦. */ ...
- Codeforces #495 Div2 problem E. Sonya and Ice Cream(1004E)
网上的大多是用树的直径做的,但是一些比较巧妙的做法,来自https://www.cnblogs.com/qldabiaoge/p/9315722.html. 首先用set数组维护每一个节点所连接的边的 ...
- 「CF1004E」Sonya and Ice Cream
题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...
- HackerRank Ice Cream Parlor
传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together ...
- 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 t ...
- Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...
- Ice Cream Tower
2017-08-18 21:53:38 writer:pprp 题意如下: Problem D. Ice Cream Tower Input file: Standard Input Output f ...
- 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心
/** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...
随机推荐
- while,格式化输出
1. while循环: while 条件: 代码块(循环体) num=1 while num<=5: print(num) num+=1 break:结束循环;停止当前本层循环 continue ...
- Leecode刷题之旅-C语言/python-69x的平方根
/* * @lc app=leetcode.cn id=69 lang=c * * [69] x 的平方根 * * https://leetcode-cn.com/problems/sqrtx/des ...
- WRITE
WRITE - int_format_options 基本形式 ... [LEFT-JUSTIFIED|CENTERED|RIGHT-JUSTIFIED] [NO-GAP] ...
- linux挂载命令mount及U盘、移动硬盘的挂载
一.mount的命令格式是(注意mount只能在root权限下运行) mount dervice dir dervice是要挂载的设备,dir是挂载点 二.查看当前磁盘列表的设备 fdisk -l 显 ...
- Ubuntu server中 samba的安装和简单配置
samba是Linux系统上的一种文件共享协议,可以实现Windows系统访问Linux系统上的共享资源,现在介绍一下如何在Ubuntu 14.04上安装和配置samba 工具/原料 Ubuntu ...
- Java:位移运算符
Java中有三个位移运算符,用于对int类型整数的二进制补码进行操作: 1. "<<": 左移运算符 在二进制补码末尾添加“0”,之前的其他位相当于左移了一位,可看作成 ...
- jq 一个强悍的json格式化查看工具
本文来自网易云社区 作者:娄超 在web 2.0时代json这种直观.灵活.高效数据格式基本已经成为一种标准格式,从各种web api,到配置文件,甚至现在连mysql都开始支持json作为数据类型. ...
- 一步一步构建手机WebApp开发——页面布局篇
继上一篇:一步一步构建手机WebApp开发——环境搭建篇过后,我相信很多朋友都想看看实战案例,这一次的教程是页面布局篇,先上图: 如上图所示,此篇教程便是教初学者如何快速布局这样的页面.废话少说,直接 ...
- OpenCV入门:(二:加载,显示,修改以及保存图片)
目标: 1.从图片文件打开图片(imread) 2.显示图片(namedWindow和imshow) 3.转换当前图片为灰色图片(cvtColor) 4.保存图片(imwrite) 代码: #incl ...
- xamdin: 添加小组件报错: render() got an unexpected keyword argument 'renderer'
查找到 xadmin里面的 dashboard.py文件内render方法,增加一个rdnderer默认参数是None一般路径在 本机虚拟环境\Lib\site-packages\xadmin\vie ...