题目来源:

https://leetcode.com/problems/network-delay-time/

自我感觉难度/真实难度:
题意:
分析:
自己的代码:
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
K-=1
nodes=collections.defaultdict(list)
for u,v,w in times:
nodes[u-1].append((v-1,w))
dist=[float('inf')]*N
dist[K]=0
done=set()
for _ in range(N):
smallest=min((d,i) for (i,d) in enumerate(dist) if i not in done)[1]
for v,w in nodes[smallest]:
if v not in done and dist[smallest]+w<dist[v]:
dist[v]=dist[smallest]+w
done.add(smallest)
return -1 if float('inf') in dist else max(dist)
代码效率/结果:

Runtime: 152 ms, faster than 83.77% of Python3 online submissions forNetwork Delay Time.

优秀代码:
class Solution:
def networkDelayTime(self, times, N, K):
"""
:type times: List[List[int]]
:type N: int
:type K: int
:rtype: int
"""
h = [(0, K)]
res = 0
reached = set()
d = {}
for u, v, w in times:
if u not in d:
d[u] = []
d[u].append([v, w])
while h:
t, n = heapq.heappop(h)
if n not in reached:
res = t
reached.add(n)
if n in d:
for v, w in d[n]:
if v not in reached:
heapq.heappush(h, [t + w, v])
if len(reached) < N:
return -1
return res

优秀的结题报告,提供了三种超级棒的解答方法:https://blog.csdn.net/fuxuemingzhu/article/details/82862769

第一次写图的代码,其实草稿纸推一推,发现也不是特别的难,不要畏惧

代码效率/结果:
自己优化后的代码:
反思改进策略:

1.对heapq的模块不熟悉,后面几天着重练习一下堆栈这一块的练习

2.对dijstra算法不熟悉,希望把上面的模块背出来

3.

float('inf')表示正无穷

float('-inf')表示负无穷

4.

dist=[float('inf')]*N

初始化List简单的办法

743. Network Delay Time的更多相关文章

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

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

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

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

  3. LeetCode 743. Network Delay Time

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

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

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

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

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

  6. [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 ...

  7. 【leetcode】Network Delay Time

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

  8. leetcode743 Network Delay Time

    """ here are N network nodes, labelled 1 to N. Given times, a list of travel times as ...

  9. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

随机推荐

  1. MPVUE - 使用vue.js开发微信小程序

    MPVUE - 使用vue.js开发微信小程序 什么是mpvue? mpvue 是美团点评前端团队开源的一款使用 Vue.js 开发微信小程序的前端框架.框架提供了完整的 Vue.js 开发体验,开发 ...

  2. 移动web模拟客户端实现多方框输入密码效果

    不知道怎么描述标题,先看截图吧,大致的效果就是一个框输入一位密码. 最开始实现的思路是一个小方框就是一个type为password的input,每输入一位自动跳到下一位,删除一位就自动跳到前一位,an ...

  3. iview select下拉bug

    1场景:弹框内有一个下拉组件(支持搜索),当选择完数据后弹框关闭,再次打开后,下拉框内的数据是刚才选中的数据.原因:分析后觉得是搜索内容没有清空,导致下拉的数据只有一个解决:调用下setQuery方法 ...

  4. 如何利用API导出带有页眉页脚的excel

     在报表中设置的页眉页脚在页面中是看不到的,如下图: 页面中的效果: 在打印的时候,可以看到页眉页脚的效果: 那么,如果将页眉页脚导入到导出的excel中呢.我们可以通过API来进行设置: < ...

  5. get_digits

    # coding=utf-8# 一.def digits(n): list1 = [] for each in n: list1.append(each) return list1print(digi ...

  6. 签署您的应用--手动签署 APK

    签署您的应用 本文内容 证书和密钥库 签署您的调试构建 调试证书的有效期 管理您的密钥 使用 Google Play 应用签名 自行管理您的密钥和密钥库 签署 APK 生成密钥和密钥库 手动签署 AP ...

  7. centos7安装magento随记 这就是个坑,果断放弃

    在centos7通过yum安装PHP7,首先在终端运行:rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm提示错误:er ...

  8. Django 开启显示查询语句log

    # 下面语句加到setti中 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': ...

  9. Linux学习之CentOS(三)----将Cent0S 7的网卡名称eno16777736改为eth0

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  10. Android组件系列----Activity组件详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...