[M最短路] lc743. 网络延迟时间(spfa最短路+单源最短路)

1. 题目来源

链接:743. 网络延迟时间

相关链接:

2. 题目解析

怎么讲呢,挺抽象的…很久没写最短路算法了。反正也是写出来了,但脱离了模板,把自己还给绕进去了…

这块还是按照模板来写吧。

至于具体的算法思想,看相关链接即可。


  • 时间复杂度 O ( n m ) O(nm) O(nm)
  • 空间复杂度 O ( 1 ) O(1) O(1)

标准 spfa

class Solution {
public:
    int networkDelayTime(vector<vector<int>>& times, int n, int k) {
        vector<pair<int, int>> g[n + 1];
        for (auto& e : times) {
            int x = e[0], y = e[1], w = e[2];
            g[x].push_back({y, w});
        }

        // 标准 spfa 算法
        queue<int> q; 
        vector<int> dist(n + 1, 1e9);   // 注意这里初始化的是最大值
        vector<bool> st(n + 1, false);
        q.push(k);
        dist[k] = 0;

        while (q.size()) {
            auto x = q.front(); q.pop();
            st[x] = false; // x 不在队列中
            for (auto& [y, w] : g[x]) { // 更新 x 点临边
                if (dist[y] > dist[x] + w) { // 如果 y 点可以被 x 点更新
                    dist[y] = dist[x] + w; // 则更新
                    if (!st[y]) { // 如果 y 不在队列中,则加入
                        q.push(y);
                        st[y] = true;
                    }
                }
            }
        }

        int res = -1e9;
        for (int i = 1; i <= n; i ++ ) {
            if (dist[i] == 1e9) return -1;  // 这里也是拿最大值进行的判断
            res = max(res, dist[i]);
        }

        return res;
    }
};

以下是 y 总写的 spfa 模板,大同小异。

const int N = 110, M = 6010, INF = 0x3f3f3f3f;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];

class Solution {
public:
    void add(int a, int b, int c) {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    }

    void spfa(int start) {
        queue<int> q;
        q.push(start);
        memset(dist, 0x3f, sizeof dist);
        dist[start] = 0;
        while (q.size()) {
            int t = q.front();
            q.pop();
            st[t] = false;
            for (int i = h[t]; ~i; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (!st[j]) {
                        q.push(j);
                        st[j] = true;
                    }
                }
            }
        }
    }

    int networkDelayTime(vector<vector<int>>& times, int n, int k) {
        memset(h, -1, sizeof h);
        idx = 0;
        for (auto& e: times) {
            int a = e[0], b = e[1], c = e[2];
            add(a, b, c);
        }
        spfa(k);
        int res = 1;
        for (int i = 1; i <= n; i ++ ) res = max(res, dist[i]);
        if (res == INF) res = -1;
        return res;
    }
};

作者:yxc
链接:https://www.acwing.com/activity/content/code/content/1011633/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

2024年11月26日00:08:57
这里不知道随便写的 spfa 也过了…
不要留下坏印象…

class Solution {
public:
    int networkDelayTime(vector<vector<int>>& times, int n, int k) {
        vector<vector<pair<int, int>>> g(n, vector<pair<int, int>>{});
        for (auto& e : times) {
            int x = e[0] - 1, y = e[1] - 1, w = e[2];
            g[x].push_back({y, w});
        }

        queue<int> q; 
        vector<int> st(n, -1);  // 即是 st 又是 dist,用 -1 做状态标记位
        k = k - 1;
        q.push(k);
        st[k] = 0;

        while (q.size()) {
            auto x = q.front(); q.pop();

            for (auto& [y, w] : g[x]) {
                if (st[y] == -1 || st[y] > st[x] + w) { // 这里其实参考的是 dij 算法,又像 spfa
                    st[y] = st[x] + w;
                    q.push(y);
                }
            }
        }

        int res = -1e9;
        for (int& x : st) {
            if (x == -1) return -1;
            res = max(res, x);
        }

        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ypuyu

如果帮助到你,可以请作者喝水~

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

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

打赏作者

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

抵扣说明:

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

余额充值