[leetcode] 743. Network Delay Time

441 篇文章 0 订阅
287 篇文章 0 订阅

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

分析

题目的意思是:给你很多三元组作为无向图的边,u是起始节点,v是终止节点,然后w是时间,现在从K发射一个信号,问所有的节点接收到这个信号需要耗费多长时间,如果不可能到达,就返回-1.

  • 当有对边 (u, v) 是结点u到结点v,如果 dist(v) > dist(u) + w(u, v),那么 dist(v) 就可以被更新,这是所有这些的算法的核心操作。Dijkstra算法是以起点为中心,向外层层扩展,直到扩展到终点为止。
  • 为了防止重复比较,我们需要使用visited数组来记录已访问过的结点,最后我们在所有的最小路径中选最大的返回(如果最长的路径上的节点都能到达,那其他路径的节点就能到达),注意,如果结果res为INT_MAX,说明有些结点是无法到达的,返回-1。
  • 普通的实现方法的时间复杂度为O(V2),基于优先队列的实现方法的时间复杂度为O(E + VlogV),其中V和E分别为结点和边的个数.

代码

class Solution {
public:
    int networkDelayTime(vector<vector<int>>& times, int N, int K) {
        int res=0;
        vector<vector<int>> edges(101,vector<int>(101,-1));
        queue<int> q{{K}};
        vector<int> dist(N+1,INT_MAX);
        dist[K]=0;
        for(auto e:times){
            edges[e[0]][e[1]]=e[2];
        }
        while(!q.empty()){
            unordered_set<int> visited;
            for(int i=q.size();i>0;i--){
                int u=q.front(); q.pop();
                for(int v=1;v<=100;v++){
                    if(edges[u][v]!=-1&&dist[u]+edges[u][v]<dist[v]){
                        if(!visited.count(v)){
                            visited.insert(v);
                            q.push(v);
                        }
                        dist[v]=dist[u]+edges[u][v];
                    }
                }
            }
        }
        for(int i=1;i<=N;i++){
            res=max(res,dist[i]);
        }
        return res==INT_MAX ? -1:res;
    }
};

参考文献

[LeetCode] Network Delay Time 网络延迟时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值