Description

给定一颗树,树中每个结点有一个邮递员,每个邮递员要沿着唯一的路径走向capital(1号结点),每到一个城市他可以有两种选择: 1.继续走到下个城市 2.让这个城市的邮递员替他出发。 每个邮递员出发需要一个准备时间W[I],他们的速度是V[I],表示走一公里需要多少分钟。 现在要你求出每个城市的邮递员到capital的最少时间(不一定是他自己到capital,可以是别人帮他) N<=100000 3 ≤ N ≤ 100 000 0 ≤ Si≤ 10^9 1 ≤ Vi≤ 10^9 The length of each road will not exceed 10 000 For 20% of the tests, N ≤ 2 500 For 50% of the tests, each town will have at most 2 adjacent roads (i.e., the graph of roads will be a line)

Input

N 以下N-1行A,B,C三个数表示A,B之间有一条长为C的边。 再N行每行两数Wi,Vi 输出有一行N-1个数表示如题所述。

f[w]=W[w]+V[w]*dep[w]+min(f[u]-dep[u]*V[w]) u在w到根的路径上

树上的斜率优化,两维分别是深度dep和答案f,dfs并用单调栈记录当前点到根路径上的凸包,三分得到决策点

为保证时间复杂度,单调栈pop时要用二分确定弹出的元素个数,并支持撤销

#include<cstdio>
typedef long double ld;
typedef long long i64;
const int N=;
int n,es[N*],enx[N*],ev[N*],e0[N],ep=;
int c[N],v[N],ss[N],sp=;
i64 f[N],dep[N];
int _(){
int x;
scanf("%d",&x);
return x;
}
bool chk(int a,int b,int w){
return (f[b]-f[a])/ld(dep[b]-dep[a])>(f[w]-f[b])/ld(dep[w]-dep[b]);
}
void f1(int w,int pa){
if(sp){
int L=,R=sp,M;
while(L<R){
M=(L+R)>>;
int a=ss[M],b=ss[M+];
if(f[a]-f[b]<v[w]*(dep[a]-dep[b]))R=M;
else L=M+;
}
f[w]=c[w]+v[w]*(dep[w]-dep[ss[L]])+f[ss[L]];
}
int L=,R=sp,M;
if(L<R&&!chk(ss[R-],ss[R],w))L=R;
while(L<R){
M=(L+R)>>;
if(chk(ss[M],ss[M+],w))R=M;
else L=M+;
}
L=sp;
M=ss[sp=R+];
ss[sp]=w;
for(int i=e0[w];i;i=enx[i]){
int u=es[i];
if(u==pa)continue;
dep[u]=dep[w]+ev[i];
f1(u,w);
}
ss[sp]=M;
sp=L;
}
int main(){
n=_();
for(int i=,a,b,c;i<n;++i){
a=_();b=_();c=_();
es[ep]=b;enx[ep]=e0[a];ev[ep]=c;e0[a]=ep++;
es[ep]=a;enx[ep]=e0[b];ev[ep]=c;e0[b]=ep++;
}
for(int i=;i<=n;++i){
c[i]=_();
v[i]=_();
}
f1(,);
for(int i=;i<=n;++i)printf("%lld%c",f[i],i==n?:);
return ;
}

bzoj 1767: [Ceoi2009]harbingers的更多相关文章

  1. BZOJ 1767] [Ceoi2009] harbingers (斜率优化)

    [BZOJ 1767] [Ceoi2009] harbingers (斜率优化) 题面 给定一颗树,树中每个结点有一个邮递员,每个邮递员要沿着唯一的路径走向capital(1号结点),每到一个城市他可 ...

  2. ●BZOJ 1767 [Ceoi2009]harbingers

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1767 题解: 斜率优化DP,单调栈,二分 定义 DP[i] 表示从 i 节点出发,到达根所花 ...

  3. bzoj1767[Ceoi2009]harbingers 斜率优化dp

    1767: [Ceoi2009]harbingers Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 421  Solved: 112[Submit][S ...

  4. [Bzoj1767][Ceoi2009]harbingers (树上斜率优化)

    1767: [Ceoi2009]harbingers Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 451  Solved: 120[Submit][S ...

  5. BZOJ1767/Gym207383I CEOI2009 Harbingers 斜率优化、可持久化单调栈、二分

    传送门--BZOJCH 传送门--VJ 注:本题在BZOJ上是权限题,在Gym里面也不能直接看,所以只能在VJ上交了-- 不难考虑到这是一个\(dp\). 设\(dep_x\)表示\(x\)在树上的带 ...

  6. BZOJ1767 : [Ceoi2009]harbingers

    设d[i]表示i到1的距离 f[i]=w[i]+min(f[j]+(d[i]-d[j])*v[i])=w[i]+d[i]*v[i]+min(-d[j]*v[i]+f[j]) 对这棵树进行点分治,每次递 ...

  7. ●BZOJ 3672 [Noi2014]购票

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3672 题解: 斜率优化DP,点分治(树上CDQ分治...) 这里有一个没有距离限制的简单版: ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. DP 优化方法大杂烩 & 做题记录 I.

    标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...

随机推荐

  1. Android JNI作用及其详解

    Android JNI作用及其详解 Java Native Interface (JNI)标准是Java平台的一部分,它允许Java代码和其他语言写的代码进行交互.JNI 是本地编程接口,它使得在 J ...

  2. [转载]oracle物化视图

    原文URL:http://lzfhope.blog.163.com/blog/static/636399220124942523943/?suggestedreading&wumii 环境or ...

  3. 用MyEclipse JPA创建项目(一)

    MyEclipse 3.15 Style——在线购买低至75折!火爆开抢>> [MyEclipse最新版下载] 本教程介绍了MyEclipse中的一些基于JPA的功能. 阅读本教程时,了解 ...

  4. redis的主从复制和高可用集群

    一.redis的简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.redis是一个key-value存储系 ...

  5. mysql left join 查询

    inner join(等值连接) 只返回两个表中联结字段相等的行 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 左连接实例: SELECT t. NAME, t1. ...

  6. vue-cli 添加less 以及sass

    1.sasscnpm i node-sass --save-dev cnpm i sass-loader --save-dev2.less npm install less --save-dev np ...

  7. matlab fgetl()

    % % file=dir('/home/wang/Desktop/trainset/others/'); % % :length(file) % % path= strcat('/home/wang/ ...

  8. [LeetCode&Python] Problem 700. Search in a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST ...

  9. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  10. test20180919 递归问题

    题意 定义 \[ f(n)=\left\{ \begin{array}{} 1 & n=1\\ f(n-f(f(n-1)))+1 & n>1 \end{array} \right ...