当前位置: 首页 > news >正文

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

文章目录

    • 1. 题目来源
    • 2. 题目解析

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 算法,又像 spfast[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;}
};
http://www.lryc.cn/news/491423.html

相关文章:

  • MySQL 中的锁
  • 【动手学电机驱动】STM32-FOC(8)MCSDK Profiler 电机参数辨识
  • 【C++11】尽显锋芒
  • 掌握控制流的艺术:Go语言中的if、for和switch语句
  • 飞书会话消息左右排列
  • .net 支持跨平台(桌面)系列技术汇总
  • springboot 静态资源访问
  • 【linux学习指南】初识Linux进程信号与使用
  • L1G1000 书生大模型全链路开源开放体系笔记
  • 亚信安全与飞书达成深度合作
  • 深入讲解Spring Boot和Spring Cloud,外加图书管理系统实战!
  • 【三维生成】Edify 3D:可扩展的高质量的3D资产生成(英伟达)
  • Java求职招聘网站开发实践
  • 一文详细了解websocket应用以及连接断开的解决方案
  • 如何做含有identify抓信号的fpga版本(image或者Bit)
  • AIGC实践-使用Amazon Bedrock的SDXL模型进行文生图
  • 【源码】Sharding-JDBC源码分析之SQL中分片键路由ShardingSQLRouter的原理
  • 初学 flutter 环境变量配置
  • 蓝牙 AVRCP 协议详解
  • 在 Ubuntu 18.04 上安装 MySQL 5.7和MySQL 8
  • 第4章 Spring Boot自动配置
  • 显存:存储,GPU:计算;Pipeline Parallelism(管道并行)
  • 费曼路径积分简单示例
  • 40分钟学 Go 语言高并发:【实战】并发安全的配置管理器(功能扩展)
  • 麒麟安全增强-kysec
  • shell编程(8)
  • 高级java每日一道面试题-2024年11月24日-JVM篇-说说对象分配规则?
  • 进程间通信5:信号
  • 性能测试及调优
  • 实战基于LangChain和ChatGLM私有化部署聊天机器人