[CF1004E] Sonya and Ice-cream
问题描述
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 ii 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 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 b. So Sonya wants to minimize
maxamin1≤i≤rda,fi
where a takes a value of all possible n junctions, fi — the junction where the ii-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.
输入格式
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.
输出格式
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.
样例输入
6 2
1 2 3
2 3 4
4 5 2
4 6 3
2 4 6
样例输出
4
解析
首先,满足要求的一条链一定在树的直径上。那么,设len为直径的长度,\(dis1[i]\)表示直径上的点i到直径左个端点的距离,\(dis2[i]\)表示直径上点i不经过直径上的点能到达的最远的点。将直径视为一个序列,并重新由左端点从1开始标号,那么,设直径有m个点,答案即为
\]
前面两个值可以直接计算得到,后面的那个max可以转化为滑动窗口问题,用单调队列解决即可。
代码
#include <iostream>
#include <cstdio>
#define N 100002
using namespace std;
int head[N],ver[N*2],nxt[N*2],edge[N*2],l;
int n,k,i,j,d[N],dis[N],d1[N],d2[N],fa[N],len,cnt,a,b,tmp,maxx,q[N],h,t;
bool vis[N];
int read()
{
char c=getchar();
int w=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w;
}
void insert(int x,int y,int z)
{
l++;
ver[l]=y;
edge[l]=z;
nxt[l]=head[x];
head[x]=l;
}
void dfs(int x,int pre)
{
fa[x]=pre;
for(int i=head[x];i;i=nxt[i]){
int y=ver[i];
if(y!=pre&&!vis[y]){
dis[y]=dis[x]+edge[i];
if(dis[y]>maxx) maxx=dis[y],tmp=y;
dfs(y,x);
}
}
}
int main()
{
n=read();k=read();
for(i=1;i<n;i++){
int u=read(),v=read(),w=read();
insert(u,v,w);
insert(v,u,w);
}
dfs(1,0);
maxx=0;dis[tmp]=0;
dfs(tmp,0);
len=maxx;
while(tmp!=0){
cnt++;
d[cnt]=tmp;
d1[cnt]=len-dis[tmp];
vis[tmp]=1;
tmp=fa[tmp];
}
for(i=1;i<=cnt;i++){
maxx=0;dis[d[i]]=0;
dfs(d[i],0);
d2[i]=maxx;
}
int ans=1<<30;
for(i=1,j=1;i<=cnt;i++){
while(h<=t&&q[h]<i) h++;
while(j<=min(i+k-1,cnt)){
while(h<=t&&d2[q[t]]<=d2[j]) t--;
q[++t]=j;
j++;
}
ans=min(ans,max(max(d1[i],len-d1[min(i+k-1,cnt)]),d2[q[h]]));
}
if(ans==1<<30) puts("0");
else printf("%d\n",ans);
return 0;
}
[CF1004E] Sonya and Ice-cream的更多相关文章
- E. Sonya and Ice Cream(开拓思维)
E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 「CF1004E」Sonya and Ice Cream
题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...
- 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数组维护每一个节点所连接的边的 ...
- 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 ...
随机推荐
- Delphi XE2 之 FireMonkey 入门(43) - 控件基础: TStringGrid、TGrid
Delphi XE2 之 FireMonkey 入门(43) - 控件基础: TStringGrid.TGrid TStringGrid.TGrid 都是从 TCustomGrid 继承; 区别有:1 ...
- 阶段2 JavaWeb+黑马旅游网_15-Maven基础_第3节 maven标准目录结构和常用命令_07maven常用命令
以给的hellowordl的的代码为例. src/main/java下,这是主业务逻辑代码 里面的内容只有一个jsp的跳转 测试包下: 里面很简单的就输出了一句话 复制项目的目录 先cd进入复制的这个 ...
- java日常统计
姓名:Danny 日期:2017/11/27 任务 日期 听课 编程程序 阅读课本 准备考试 日统计 周一 30 120 150 周二 50 140 190 周三 周四 周五 周六 周 ...
- 初学node.js-nodejs中实现修改用户路由
经过前面几次的学习,已经可以做下小功能,今天要实现的是修改用户路由. 一.users_model.js 功能:定义用户对象模型 var mongoose=require('mongoose'), S ...
- Chapter03 第三节 浮点数
3.3 浮点数 3.3.1 浮点数的表示 常规表示:12.34.0.01.8.0 E表示: 2.5e+8(2.5 10^8).7E6(7.0 10^6) (e大小写随意) (e+x或者E-x表示小数点 ...
- 应用安全 - Web框架 - 数据库管理 - phpMyAdmin - 漏洞汇总
CVE-2019-18622 Date: 2019.10.28 类型: SQL injection in Designer feature 影响范围: phpMyAdmin versions prio ...
- js swich
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- python 并发编程 多进程 模拟抢票
抢票是并发执行 多个进程可以访问同一个文件 多个进程共享同一文件,我们可以把文件当数据库,用多个进程模拟多个人执行抢票任务 db.txt {"count": 1} 并发运行,效率高 ...
- [转帖]青岛uber偷拍设备
爱彼迎民宿路由器暗藏摄像头:官方回应已移除房源 https://www.cnbeta.com/articles/tech/844233.htm 罚款 就是搞笑啊.. 不过现在偷拍设备真多... 5月5 ...
- 极*Java速成教程 - (7)
Java高级特性 数组 在Java中,数组是一串连续的,不可改变长度的,对象被固定的,类型固定的连续空间.数组中的随机访问非常迅速,但为了速度放弃了灵活性.而效率也是数组最大的优点. 在使用泛型的容器 ...