743. Network Delay Time

Description

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.

Solution

Djikstra

It is a direct graph.

  • Use Map<Integer, Map<Integer, Integer>> to store the source node, target node and the distance between them.
  • Offer the node K to a PriorityQueue.
  • Then keep getting the closest nodes to the current node and calculate the distance from the source (K) to this node (absolute distance). Use a Map to store the shortest absolute distance of each node.
  • Return the node with the largest absolute distance.
class Solution {
    public int networkDelayTime(int[][] times, int N, int K) {
        // build a directed graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 1; i <= N; ++i) {
            graph.put(i, new HashMap<>());
        }
        
        for (int[] time : times) {
            graph.get(time[0]).put(time[1], time[2]);
        }
        
        Map<Integer, Integer> timeMap = new HashMap<>();
        PriorityQueue<Map.Entry<Integer, Integer>> queue 
            = new PriorityQueue<>((a, b) -> a.getValue() - b.getValue());
        queue.addAll(graph.get(K).entrySet());
        
        int maxTime = 0;
        
        while (!queue.isEmpty()) {
            Map.Entry<Integer, Integer> entry = queue.poll();
            int node = entry.getKey();
            int time = entry.getValue();
            
            if (timeMap.containsKey(node)) {
                continue;
            }
            
            timeMap.put(node, time);
            maxTime = Math.max(maxTime, time);
            
            for (Map.Entry<Integer, Integer> next : graph.get(node).entrySet()) {
                next.setValue(next.getValue() + time);
                queue.offer(next);
            }
        }
        
        return timeMap.size() == N - 1 ? maxTime : -1;  // in case there's a node that K cannot reach
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,164评论 0 10
  • 文 | 一鸣 【个人写作分享】你还在用Word写小说?码字人的“武器”灵感不足?技巧助攻!——一鸣的创作经验分享...
    一鸣阅读 11,094评论 15 171
  • 商业问题13:租金太高,无法为新的分公司找到合适的办公楼。 金刚解药:当别人需要落脚之处的时候,务必帮助他们找寻。...
    柔光宝宝阅读 1,196评论 0 0
  • ——给张昭的一封信 本文参加# 印象青农,萌有感受#活动,本文承诺,文章内容为原创,且未在其他平台发表过...
    天下御免阅读 2,523评论 2 6
  • 集资诈骗罪,是指以非法占有目的,使用诈骗方法非法集资,数额较大的行为。 本罪必须以非法占有为目的,如果诈骗数额不大...
    d9a7bf245ece阅读 3,388评论 0 0