leetcode-743-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:

    N will be in the range [1, 100].
    K will be in the range [1, N].
    The length of times will be in the range [1, 6000].
    All edges times[i] = (u, v, w) will have 1 <= u, v <= N and 0 <= w <= 100.


题意:求出网络中 从指定的 某个点到其他点的 最短距离中的最大距离。

本质:根据每个点,动态计算相邻最近的点到目标点的距离,并以此更改其他点的目标距离;

           dijkstra算法,求到所有node的最短路径中最长的某一个。

           从某点出发,先记录相邻的node的距离,然后逐次往外扩散,找与相邻node相邻的node。

应用:场景条件--节点众多,不相邻的节点与节点之间的路径众多,找出最短的。

            思路:既然路径有多条,那每次遍历一个node,就将所有可能的情况比较一下,找一个总体距离最短的记录下来,如此往复。

相似:动态规划,每次只考虑最近的情况进行累加,找到可以复用的办法。

 

 

 

 

package com.jd.jr.nlp;


import java.util.Arrays;

/***
 *
 * graph 存储所有点之间的关系
 * distance 存储所有点跟目标点之间的距离
 *
 * 动态规划,逐层扩散, 层是指跟target接触的点。更新后 有值的点也是跟target接触的点。
 *
 */

public class Solution743Diy {

    public int networkDelayTime(int[][] times, int N, int K) {
        int[][] graph=new int[N+1][N+1];
        int[] distance=new int[N+1];
        boolean[] visited=new boolean[N+1];

        for(int i=0;i<=N;i++){
            for(int j=0;j<=N;j++){
                graph[i][j]=-1;
            }
        }

        Arrays.fill(distance,-1);
        distance[K]=0;

        for(int[] time:times){
            graph[time[0]][time[1]]=time[2];
        }

        for(int i=1;i<=N;i++){
            if(graph[K][i]!=-1){
                distance[i]=graph[K][i];
            }
        }

        Arrays.fill(visited,false);


        for(int i=0;i<N;i++){
            int minDistance=Integer.MAX_VALUE;
            int minIndex=1;
            for(int j=1;j<=N;j++){
                if(j==K){
                    continue;
                }
                if(!visited[j] && distance[j]!=-1 && distance[j]<minDistance){
                    minIndex=j;
                    minDistance=distance[j];
                }
            }

            visited[minIndex]=true;

            for(int j=1;j<=N;j++){
                if(graph[minIndex][j]!=-1 ){
                    if(distance[j]!=-1){
                    distance[j]=Math.min(distance[j],distance[minIndex]+graph[minIndex][j]);
                }else {
                    distance[j]=distance[minIndex]+graph[minIndex][j];
                }
                }
            }
        }
        int maxDistance=0;
        for(int i=1;i<=N;i++){
            if (distance[i] == -1) {
                return -1;
            }
            if(distance[i]>maxDistance){
                maxDistance=distance[i];
            }
        }
        return maxDistance;
    }

    public static void main(String[] args){
//        times = [[2,1,1],[2,3,1],[3,4,1]], N = 4, K = 2
//        List<List<Integer>> times=new ArrayList<>();
//        times.add(Arrays.asList(2,1,1));
//        times.add(Arrays.asList(2,3,1));
//        times.add(Arrays.asList(3,4,1));

//        int[][] times=new int[3][3];
        int[][] times=new int[][]{{2,1,1},{2,3,1},{3,4,1}};
//        times[0]={2,1,1};

        int N=4;
        int K=2;
        Solution743Diy solution743=new Solution743Diy();
        int result=solution743.networkDelayTime(times,N,K);
        System.out.println("=======result=======");
        System.out.println(result);

    }



}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值