• Dijkstra Java


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

    /* Java program to find a Pair which has maximum score*/
    import javafx.util.Pair;
    
    class Solution {
        
        
        public int networkDelayTime(int[][] times, int N, int K) {
            
            Map<Integer,List<int[]>> graph = new HashMap<>();
            
            for(int[] edge:times)
            {
                if(!graph.containsKey(edge[0]))
                    graph.put(edge[0],new ArrayList<int[]>());
                graph.get(edge[0]).add(new int[]{edge[1],edge[2]});
            }
            
            Map<Integer,Integer> node_dis = new HashMap<>();
            
            for(int i =1;i<=N;i++)
                node_dis.put(i,Integer.MAX_VALUE);
            node_dis.put(K,0);
            
            boolean[] seen = new boolean[N+1];
            
            while(true)
            {
                int curNode = -1;
                //find the nearest node
                int curDis = Integer.MAX_VALUE;
                for(int i =1;i<=N;i++)
                {
                    if(!seen[i] && node_dis.get(i)<curDis)
                    {
                        curDis = node_dis.get(i);
                        curNode = i;
                    }
                }
                
                if(curNode==-1) break;
                seen[curNode] = true;
                if(graph.containsKey(curNode))
                    for(int[] pair: graph.get(curNode))
                    {
                        node_dis.put(pair[0],Math.min(node_dis.get(pair[0]), curDis+pair[1]));
                    }
            }
            
            
            int res =0;
            for(int d: node_dis.values())
            {
                if(d==Integer.MAX_VALUE) return -1;
                res = Math.max(res,d);
            }
            
            
            return res;
        }
    }
  • 相关阅读:
    AngularJS 包含HTML文件
    AngularJS 验证
    AngularJS html+DOM+ng-click事件
    表格边框css
    Ubantu下面命令听歌(豆瓣fm)
    AngularJS $http
    AngularJS过滤器
    Python-注册
    Python之内置函数
    生成手机号码代码
  • 原文地址:https://www.cnblogs.com/qlky/p/9967209.html
Copyright © 2020-2023  润新知