Gym 101915G Robots
The Robotics Olympiad teams were competing in a contest.
There was a tree drawn on the floor, consisting of n nodes and n - 1 edges. The nodes are numbered from 1 to n, and each edge has a weight. The tree is rooted at the first node. q teams are participating, and each team is given an integer xi. Their robot should start at node 1, and move in the following way until there are no valid moves left: From all the edges between the current node and it's children, go through the edge with the maximum value less than xi. Note that the robot can't move to the parent, only to children.
However, the teams weren't able to program the robots to return to them after the contest, so they had to manually pick them up. Since the tree can be quite large, they need your help to determine where each robot ended it's movement.
The first line contains a single integer T, the number of test cases.
The first line of each test case contains two space-separated integers n and q. (1 ≤ n, q ≤ 105).
The following n - 1 lines contain 3 integers ui, vi, wi. This means that there is an edge connecting nodes ui and vi, with weight wi. (1 ≤ ui, vi ≤ n) (1 ≤ wi ≤ 109). It's guaranteed that all wi are distinct.
The following line contains q integers xi. (1 ≤ xi ≤ 109).
For each test case, print one line with a single number Si, the sum of numbers of nodes where each robot ends.
In the sample test case, the robots end in the following nodes: {1, 1, 2, 5, 5, 3, 4}.
Si = 1+1+2+5+5+3+4 = 21.
Large I/O files. Please consider using fast input/output methods.
【题意】
有n个点,n-1条边,每条边有权值。现在有M个机器人,他们也有自己的权值,他们会从点1开始顺着这颗树向下走,每个机器人能走的边是那些边的权值严格小于机器人权值的边。但是机器人是有选择的,它只会走那些能选择的边里面权值尽可能大的边走,并且它们是无法回头的。
现在询问给定的M个机器人最后停留在哪些点,求这些点的和
【分析】
把每个点连出去的边按照权值降序排序,这样保证在之后的遍历中优先选择边权大的(贪心)。每个机器人的最后停留位置是固定的,所以他们谁先走的顺序是无所谓的,只有权值才是我们需要的。所以按照机器人的权值从大到小也排序一遍,这里用优先队列来更新这个信息。
首先预处理出达到每个点所需要的最大的权值大小用mx[ ]记录。再按照DFS的顺序到达每个无法再向下的点的时候比较该点的mx[ i
]值与当前权值最大的机器人的权值,若机器人的权值大于mx[ i
]则表示这个机器人是可以到达这里的,此时更新答案并且把这个机器人从队列中取出。这样操作直到队头元素不满足权值大于mx[ i
],然后开始回溯,这样就可以保证每个机器人一定是达到了他们固定的位置。
【代码】
__EOF__

本文链接:https://www.cnblogs.com/shenben/p/10371952.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
2017-02-13 股神小L 2016Vijos省选集训 day1
2017-02-13 兔子的晚会 2016Vijos省选集训 day1
2017-02-13 1194: [HNOI2006]潘多拉的盒子
2017-02-13 Circling Round Treasures CodeForces - 375C
2017-02-13 POJ3182 The Grove[射线法+分层图最短路]
2017-02-13 大白第一章第四节dp例题