力扣labuladong——一刷day84

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

Dijkstra 算法(一般音译成迪杰斯特拉算法)无非就是一个 BFS 算法的加强版,它们都是从二叉树的层序遍历衍生出来的


一、力扣743. 网络延迟时间

class Solution {
   
    public int networkDelayTime(int[][] times, int n, int k) {
   
        List<int[]>[] graph = new LinkedList[n+1];
        for(int i = 1; i <= n; i ++){
   
            graph[i] = new LinkedList<int[]>();
        }
        for(int[] time : times){
   
            int from = time[0];
            int to = time[1];
            int w = time[2];
            graph[from].add(new int[]{
   to,w});
        }
        int[] result = dijkstra(k,graph);
        int res = 0;
        for(int i = 1; i < result.length; i ++){
   
            if(result[i] == Integer.MAX_VALUE){
   
                return -1;
            }
            res = Math.max(res,result[i]);
        }
        return res;
    }
    class State{
   
        int id;
        int distFromStart;
        public State(int id, int distFromStart){
   
            this.id = id;
            this.distFromStart = distFromStart;
        }
    }
    public int[] dijkstra(int start, List<int[]>[] graph){
   
        int n = graph.length;
        int[] result = new int[n];
        PriorityQueue<State> pq = new PriorityQueue<>(
            (a,b)->{
   
                return a.distFromStart - b.distFromStart;
            }
        );
        Arrays.fill(result,Integer.MAX_VALUE);
        result[start] = 0;
        pq.offer(new State(start,0));
        while(!pq.isEmpty()){
   
            State curV = pq.poll();
            int curId = curV.id;
            int curDistFromStart = curV.distFromStart;
            if(curDistFromStart > result[curId]){
   
                continue;
            }
            for(int[] next : graph[curId]){
   
                int nextId = next[0];
                int nextDist =  result[curId] + next[1];
                if(result[nextId] > nextDist){
   
                    result[nextId] = nextDist;
                    pq.offer(new State(nextId, result[nextId]));
                }
            }
        }
        return result;
    }
}

相关推荐

  1. labuladong——day84

    2024-01-04 16:14:38       54 阅读
  2. labuladong——day80

    2024-01-04 16:14:38       57 阅读
  3. labuladong——day81

    2024-01-04 16:14:38       58 阅读
  4. labuladong——day87

    2024-01-04 16:14:38       58 阅读
  5. labuladong——day89

    2024-01-04 16:14:38       53 阅读
  6. labuladong——day68

    2024-01-04 16:14:38       56 阅读
  7. labuladong——day67

    2024-01-04 16:14:38       60 阅读
  8. labuladong——day69

    2024-01-04 16:14:38       56 阅读
  9. labuladong——day66

    2024-01-04 16:14:38       51 阅读
  10. labuladong——day70

    2024-01-04 16:14:38       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-04 16:14:38       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-04 16:14:38       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-04 16:14:38       82 阅读
  4. Python语言-面向对象

    2024-01-04 16:14:38       91 阅读

热门阅读

  1. 招贤令:一起来搞一个新开源项目

    2024-01-04 16:14:38       56 阅读
  2. LeetCode[27]移除元素

    2024-01-04 16:14:38       64 阅读
  3. 【知识积累|深度度量学习】open-metric-learning简介

    2024-01-04 16:14:38       68 阅读
  4. js批量导入获取xlsx文件数据

    2024-01-04 16:14:38       62 阅读