"""
here 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.
Example 1:
Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
Output: 2
"""
"""
此题很经典,是求源点到目标结点的最短路径
Dijkstra算法,纯模板题。
时间复杂度是O(N^2 + E),空间复杂度是O(N+E).
"""
class Solution1:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
dist = [float('inf')] * N
dist[K - 1] = 0 #K-1是存到距离数组的下标
for i in range(N): #N个结点,遍历N遍更新最短路径
for time in times:
u = time[0] - 1 #减1是为了dist的下标一致
v = time[1] - 1
w = time[2]
dist[v] = min(dist[v], dist[u] + w) #更新最短路径
return -1 if float('inf') in dist else max(dist) ss = Solution1()
ss.networkDelayTime([[1, 2, 1], [2, 3, 7], [1, 3, 4], [2, 1, 2]], 3, 2)

leetcode743 Network Delay Time的更多相关文章

  1. 【LeetCode】743. Network Delay Time 解题报告(Python)

    [LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  2. [Swift]LeetCode743. 网络延迟时间 | Network Delay Time

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...

  3. [LeetCode] Network Delay Time 网络延迟时间

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...

  4. [LeetCode] Network Delay Time 网络延迟时间——最短路算法 Bellman-Ford(DP) 和 dijkstra(本质上就是BFS的迭代变种)

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edges ti ...

  5. 743. Network Delay Time

    题目来源: https://leetcode.com/problems/network-delay-time/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: class Solution: ...

  6. [LeetCode] 743. Network Delay Time 网络延迟时间

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...

  7. LeetCode 743. Network Delay Time

    原题链接在这里:https://leetcode.com/problems/network-delay-time/ 题目: There are N network nodes, labelled 1  ...

  8. 【leetcode】Network Delay Time

    题目: There are N network nodes, labelled 1 to N. Given times, a list of travel times as directed edge ...

  9. 排队时延(Queuing delay)

    网络时延的构成 Network delay including four parts: Processing delay - time routers take to process the pack ...

随机推荐

  1. pandas中数据框DataFrame获取每一列最大值或最小值

    1.python中数据框求每列的最大值和最小值 df.min() df.max()

  2. jsp格式化日期

    1.先引入JSTL库 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> ...

  3. Python磁力获取器命令行工具 torrent-cli

    作为一个搞代码的,找资源这种事肯定不能像普通人一样打开百度盲目查找,你需要写个爬虫工具来帮你完成这件事情啦! 兼容环境 Windows/Linux/MacOs 安装 pip 安装 $ pip inst ...

  4. JavaScript - map和parseInt的坑

    问题: var arrs = ['1', '2', '3']; var r = arrs.map(parseInt); alert(r);//1,NaN,NaN map arr.map(functio ...

  5. solve License Key is legacy format when use ACTIVATION_CODE activate jetbrains-product 2019.3.1

    1.the java-agent and ACTIVATION_CODE can get from this site:https://zhile.io/2018/08/25/jetbrains-li ...

  6. SpringBoot报错笔记

    异常一: 1.访问所有方法路径都返回一个page: 截图: 出错原因:不知道 解决方法:新建项目 异常二: 提交表单信息报错 原因:映射文件和和表单的提交方式不统一 解决方法:统一方式即可: 错误三: ...

  7. JAVA高级编程数据源datasource

    原文链接 数据源 通过jdbc连接数据库,多建立几条连接放在数据源里面.可以设置数据源的最大连接数,同时活跃的连接数,最少空闲的连接数,能够同时接收处理的连接数等等. dbcp数据源 需要的jar包: ...

  8. elk单机安装部署

    es 下载地址:wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.1.0-linux-x86_64.t ...

  9. oracle SQL 练习

    COURSE 表 DROP TABLE "SCOTT"."course"; CREATE TABLE "SCOTT"."cours ...

  10. Python中self的用法

    在Python类中规定,函数的第一个参数是实例对象本身,并且约定俗成,把其名字写为self.其作用相当于java中的this,表示当前类的对象,可以调用当前类中的属性和方法. 在python中,类是通 ...