leetcode 743 网络延迟时间 Dijkstra算法
2021-03-01 22:31
标签:array 时间 key public 效果 nal inf == number JAVA 暴力解法: JAVA 暴力解法由自底向上转为自顶向下,方便剪枝: JAVA 贪心解法(Dijkstra算法 ): JS 贪心解法(Dijkstra算法): 最终解法效果: leetcode 743 网络延迟时间 Dijkstra算法 标签:array 时间 key public 效果 nal inf == number 原文地址:https://www.cnblogs.com/niuyourou/p/14423782.html public final int networkDelayTime(int[][] times, int n, int k) {
Map
public final int networkDelayTime(int[][] times, int n, int k) {
Map
public final int networkDelayTime(int[][] times, int n, int k) {
Map
var networkDelayTime = function (times, n, k) {
let map = new Map(), len = n + 1, shortest = new Array(len), re = -1, isShortest = new Array(len);
for (let i = 0; i ) {
if (!map.has(times[i][0])) map.set(times[i][0], []);
map.get(times[i][0]).push([times[i][1], times[i][2]]);
}
for (let i = 1; i Number.MAX_VALUE;
for (let i = 1; i false;
shortest[k] = 0;
while (true) {
let currNode = -1, currShortest = Number.MAX_VALUE;
for (let i = 0; i ) {
if (!isShortest[i] && currShortest > shortest[i]) {
currShortest = shortest[i];
currNode = i;
}
}
if (currNode === -1) break;
isShortest[currNode] = true;
let currNextArr = map.get(currNode);
if (!currNextArr) continue;
for (let i = 0; i ]);
}
for (let i = 1; i ) {
if (shortest[i] == Number.MAX_VALUE) return -1;
re = Math.max(re, shortest[i]);
}
return re;
};
上一篇:数组的使用
下一篇:JavaScript基础
文章标题:leetcode 743 网络延迟时间 Dijkstra算法
文章链接:http://soscw.com/index.php/essay/58757.html