"""
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. 吴裕雄 python 神经网络——TensorFlow TFRecord样例程序

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  2. Cat4500升级注意事项

    第一部分:升级ROMMON 可以通过release note去查看什么Supervisor建议什么样的版本.例如:ROMMON Release Notes for the WS-X45-SUP7-E, ...

  3. WLC HA-维护模式(Maintenance Mode)

    为什么会进入维护模式?试想一下,当一个HA组网中的两个控制器,发生了故障,两个WLC都为active,是否会出现问题?可以想象AP是什么情况,所以,WLC进入维护模式是为了避免脑裂的情况,为了避免这种 ...

  4. float元素上-margin的用法

    css标准,float元素上的负margin表示把下面的元素向对应方向移动,并且覆盖上一个元素(这里是指html中元素的声明顺序). 标准情况下我们用float 时候是这样的. -margin通俗点说 ...

  5. Bcrypt加密算法简介

    用户表的密码通常使用MD5等不可逆算法加密后存储,为防止彩虹表破解更会先使用一个特定的字符串(如域名)加密,然后再使用一个随机的salt(盐值)加密. 特定字符串是程序代码中固定的,salt是每个密码 ...

  6. matlab练习程序(传染病模型)

    最近新型冠状病毒疫情越来越严重了,待在家中没法出去,学习一下经典传染病模型. 这里总结了五个模型,分别是SI模型,SIS模型,SIR模型,SIRS模型,SEIR模型. 这几种模型的特点先介绍一下. 首 ...

  7. UICollectionView特殊布局

    流式http://www.code4app.com/thread-30894-1-1.html 美团http://www.code4app.com/thread-30930-1-1.html 复杂布局 ...

  8. 最优矩阵连乘问题 区间DP

    最优矩阵连乘积 Accepted: 10 Total Submit: 18Time Limit: 1000ms Memony Limit: 32768KB Description 在科学计算中经常要计 ...

  9. mybatis利用generator自动生成的代码

    /** * 排序规则 */ protected String orderByClause; /** * 去重规则 */ protected boolean distinct; /** * where条 ...

  10. 安装本地jar到maven仓库

    mvn install:install-file -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar -D ...