博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
743. Network Delay Time(最短路径)
阅读量:5133 次
发布时间:2019-06-13

本文共 2475 字,大约阅读时间需要 8 分钟。

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:

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

Solution1: floyd(TLE)

class Solution:    def networkDelayTime(self, times, N, K):        """        :type times: List[List[int]]        :type N: int        :type K: int        :rtype: int        """        ans = [[float('inf') for i in range(N)] for _ in range(N)]        for time in times:            ans[time[0]-1][time[1]-1] = time[2]        for i in range(N):            ans[i][i] = 0        for k in range(N):            for i in range(N):                for j in range(N):                    ans[i][j] = min(ans[i][j],ans[i][k]+ans[k][j])        return -1 if float('inf') in ans[K-1] else max(ans[K-1])

Solution2:Bellman-Ford(TLE)

class Solution:    def networkDelayTime(self, times, N, K):        """        :type times: List[List[int]]        :type N: int        :type K: int        :rtype: int        """        ans = [float('inf') for _ in range(N)]        ans[K-1] = 0        for i in range(N):            for time in times:                u,v,w = time[0]-1,time[1]-1,time[2]                ans[v] = min(ans[v],ans[u] + w)        return -1 if float('inf') in ans else max(ans)

Solution3:dijkstra

from collections import defaultdictclass Solution:    def networkDelayTime(self, times, N, K):        """        :type times: List[List[int]]        :type N: int        :type K: int        :rtype: int        """        K -= 1        nodes = defaultdict(list)        for u,v,w in times:            nodes[u-1].append((v-1,w))        ans = [float('inf') for _ in range(N)]        ans[K] = 0        s = set()        for _ in range(N):            smallest = min((d,i) for (i,d) in enumerate(ans) if i not in s)[1]            for v,w in nodes[smallest]:                if v not in s and ans[smallest]+w < ans[v]:                    ans[v] = ans[smallest] + w            s.add(smallest)        return -1 if float('inf') in ans else max(ans)

转载于:https://www.cnblogs.com/bernieloveslife/p/9774659.html

你可能感兴趣的文章
NAT基本原理
查看>>
Java Content Repository API 简介 转自(https://www.ibm.com/developerworks/cn/java/j-jcr/)
查看>>
visio二次开发——图纸解析
查看>>
Activity之间的跳转:
查看>>
iTunes Connect 开发者上手经验(转)
查看>>
vertical-align你为什么不生效
查看>>
request.getReader()的怪异事件
查看>>
C++ 实践总结
查看>>
composer 国内镜像配置
查看>>
软件是天时、地利、人和的产物!
查看>>
python定时清空本目录下除本脚本外的全部文件
查看>>
【PHP】在目标字符串指定位置插入字符串
查看>>
【JS】jQuery设置定时器,访问服务器(PHP示例)配合微信、支付宝原生支付,跳转web网页...
查看>>
实验四2
查看>>
在小程序开发的新风口 看华为云如何助立创科技抢占市场红利
查看>>
第一次博客随笔:苏钰冰
查看>>
HIS-DELPHI-读取数据库配置
查看>>
如何引入iconfont图标与Element-UI组件
查看>>
ArcMap合并之路 -- 该段路合并成一个完整的路
查看>>
在UC浏览器打开链接唤醒app,假设没有安装该app,则跳转到appstore下载该应用
查看>>