zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    animation-fill-mode
    css3 media queries
    三列,左右两列宽度固定,中间宽度自适应
    两列布局,一列定宽,一列宽度自适应
    css3 animation
    transition和animation区别
    transform 和 transition
    弹出框样式
    python map和filter函数
    leetcode Z字形字符串
  • 原文地址:https://www.cnblogs.com/qlky/p/9967209.html
Copyright © 2011-2022 走看看