leetcode742

一、Dijkstra

 public int networkDelayTime(int[][] times, int n, int k) {
        int inf=0x3f3f3f3f;
        int[][] weight=new int[n+1][n+1];
        int[] distance=new int[n+1];
        int[] visited=new int[n+1];
        HashMap<Integer,List<Integer>> map=new HashMap<>();
        for (int i=1;i<=n;i++){
            map.put(i,new ArrayList<>());
        }
        for (int[] time:times){
            int x=time[0];
            int y=time[1];
            weight[x][y]=time[2];
            map.get(x).add(y);
        }
        PriorityQueue<int[]> pq=new PriorityQueue<>(new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1]-o2[1];
            }
        });
        Arrays.fill(distance,inf);
        distance[k]=0;
        pq.add(new int[]{k,0});
        while (!pq.isEmpty()){
            int[] temp=pq.poll();
            int base=temp[0];
            int dis=temp[1];
            if (visited[base]==1){
                continue;
            }
            visited[base]=1;
            distance[base]=dis;
            for (int x:map.get(base)){
                if (visited[x]==0&&dis+weight[base][x]<distance[x]){
                    pq.add(new int[]{x,dis+weight[base][x]});
                }
            }
        }
        int ans = 0;
        for (int i = 1; i < distance.length; i++) {
            int t = distance[i];
            if (t >=inf) {
                return -1;
            }
            ans = Math.max(ans, t);
        }
        return ans;
}

二、邻接矩阵

class Solution {
    public int networkDelayTime(int[][] times, int n, int k) {
        // hash时间
        int[][] ws = new int[101][101];
        // 构建邻接边
        Map<Integer, List<Integer>> edges = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            edges.put(i, new ArrayList<>());
        }
        for (int i = 0; i < times.length; i++) {
            int[] time = times[i];
            int u = time[0], v = time[1], w = time[2];
            ws[u][v] = w;
            edges.get(u).add(v);
        }
        // 抵达的最早时间
        int[] arrival = new int[n + 1];
        Arrays.fill(arrival, Integer.MAX_VALUE);
        arrival[k] = 0;
        // queue.add(new int[]{抵达点,抵达时间})
        PriorityQueue<int[]> queue = new PriorityQueue<>((a, b) -> a[1] - b[1]);
        queue.add(new int[] { k, 0 });
        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int u = cur[0], w = cur[1];
            // 早就抵达过了,舍弃
            if (w > arrival[u]) {
                continue;
            }
            arrival[u]=w;
            List<Integer> nexts = edges.get(u);
            for (int next : nexts) {
                int t = w + ws[u][next];
                // 更新最早抵达时间
                if (t < arrival[next]) {
                    // arrival[next] = t;
                    queue.add(new int[] { next, t });
                }
            }
        }
        int ans = 0;
        for (int i = 1; i < arrival.length; i++) {
            int t = arrival[i];
            if (t == Integer.MAX_VALUE) {
                return -1;
            }
            ans = Math.max(ans, t);
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值