【第十七周】743. Network Delay Time

原题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
摘要由CSDN通过智能技术生成

原题

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.

Note:
N will be in the range [1, 100].
K will be in the range [1, N].
The length of times will be in the range [1, 6000].
All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 1 <= w <= 100.

leetCode地址:https://leetcode.com/problems/network-delay-time/description/

解题思路

题目大意:有N个网络节点,给出相互连接的线路以及线路耗时;现选择某个节点,向其所有邻居发送信号,且任何节点接收到信号后都会转发出去。求从信号产生开始,到最后一个节点收到信号的最短时间。如果有节点不能收到信号,返回-1。

这个信号的传播类似路由器的工作原理,转发路由表就是这种情况。
要求的是到达最后一个节点的最短时间,很明显我们可以发现,这是一个类似求单源最短路的问题。
先通过给出的边来构建带权图,然后选择一个节点起始,对其余所有节点都求最短路径;求得最大的路径值就是需要的耗时。
求单源最短路的算法有很多,类似Dijkstra算法、Floyd算法等都可以完成,我选择的是Dijkstra算法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值