LeetCode 743. Network Delay Time

原题链接在这里:https://leetcode.com/problems/network-delay-time/

题目:

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.

Example 1:

Input: times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
Output: 2

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 0 <= w <= 100.

题解:

Construct a graph. Then use the shortest time to traverse to the point.

Have a minHeap based on the time spent to get to the point. With the graph and current point, find all the next points and calculate the time to get to that point, current.time + time from current to next point, put it to minHeap.

Thus, we could always get to a point with shortest time.

Note: Update visited set when poll out of minHeap but not before adding to the heap. Otherwise, if it takes very long time to next point and  add the next point to visited, later if there is shorter path to that point, it can't be added to minHeap.

So update visited set after polling out of minHeap and update res if it is not visited before.

Time Complexity: O(E+VlogV). It takes O(E) time to construct graph. O(VlogV) time to traverse all the points.

Space: O(E+V). O(E) for graph, O(V) for minHeap and Set.

AC Java:

 1 class Solution {
 2     public int networkDelayTime(int[][] times, int N, int K) {
 3         Map<Integer, List<int []>> graph = new HashMap<>();
 4         for(int [] edge : times){
 5             graph.putIfAbsent(edge[0], new ArrayList<int []>());
 6             graph.get(edge[0]).add(new int[]{edge[1], edge[2]});
 7         }
 8         
 9         int res = 0;
10         
11         PriorityQueue<int []> minHeap = new PriorityQueue<int []>((a,b) -> a[0]-b[0]);
12         minHeap.add(new int[]{0, K});
13         Set<Integer> visited = new HashSet<Integer>();
14         int count = 0;
15         
16         while(!minHeap.isEmpty()){
17             int[] cur = minHeap.poll();
18             if(visited.contains(cur[1])){
19                 continue;
20             }
21             
22             visited.add(cur[1]);
23             count++;
24             res = cur[0];
25             if(graph.containsKey(cur[1])){
26                 for(int [] next : graph.get(cur[1])){
27                     minHeap.add(new int[]{res+next[1], next[0]});
28                 }
29             }
30         }
31         
32         return count == N ? res : -1;
33     }
34 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11162317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值