743. Network Delay Time
/** * 743. Network Delay Time * https://leetcode.com/problems/network-delay-time/description/ * https://blog.csdn.net/afei__/article/details/83780362 * https://www.cnblogs.com/grandyang/p/8278115.html * * There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges times[i] = (u, v, w), where u is the source node, v is the target node, and w is the time it takes for a signal to travel from source to target. Now, we send a signal from a certain node K. How long will it take for all nodes to receive the signal? If it is impossible, return -1 * */ class NetCode constructor(u_: Int) { var u = 0 val neighbors = HashMap<Int, Int>()//target u and weight var distance = Int.MAX_VALUE init { this.u = u_ } } class Solution { fun networkDelayTime(times: Array<IntArray>, N: Int, K: Int): Int { val map = HashMap<Int, NetCode>() //优先队表,sort from small to big val queue = PriorityQueue<NetCode>(N) { o1, o2 -> o1.distance - o2.distance } //init for (i in 1..N) { val node = NetCode(i) if (i == K) { //the start node.distance = 0 } map.put(i, node) queue.offer(node) } //update neighbor node for (time in times) { val node = map.get(time[0])//(u, v, w) if (node != null) { node.neighbors.put(time[1], time[2])//u,v } } //use dijkstra while (!queue.isEmpty()) { //the head is the node which smallest distance in PriorityQueue val min = queue.poll() if (min.distance == Int.MAX_VALUE) { return -1 } //relax:更新某个顶点的所有邻据顶点的distance for (v in min.neighbors.keys) { val curr = map.get(v) val distance = min.distance + min.neighbors.get(v)!!//get the weight if (curr != null) { if (curr.distance > distance) { //set the small value curr.distance = distance //update the position of curr in queue queue.remove(curr) queue.add(curr) } } } }//end while //find the max var max = 0 for (node in map.values) { if (node.distance > max) { max = node.distance } } return max } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)