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

AcWing 1129. 热浪(单源最短路)

题目链接

https://www.acwing.com/problem/content/1131/icon-default.png?t=N7T8https://www.acwing.com/problem/content/1131/

题解

此题属于单源最短路问题,根据数据范围,可以使用Dijkstra算法、堆优化版的Dijkstra算法、SPFA算法。本例采用SPFA算法,使用手写循环队列来实现。

代码

#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;const int N = 2510, M = 6200 * 2 + 10;int n, m, S, T;
int h[N], e[M], w[M], ne[M], idx;
int dist[N], q[N];
bool st[N];void add(int a, int b, int c)
{e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}void spfa()
{memset(dist, 0x3f, sizeof dist);dist[S] = 0;int hh = 0, tt = 1;q[0] = S, st[S] = true;while (hh != tt){int t = q[hh++];if (hh == N) hh = 0;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[tt++] = j;if (tt == N) tt = 0;st[j] = true;}}}}
}int main()
{cin >> n >> m >> S >> T;memset(h, -1, sizeof h);for (int i = 0; i < m; i++){int a, b, c;cin >> a >> b >> c;add(a, b, c), add(b, a, c);}spfa();cout << dist[T] << endl;return 0;
}

参考资料

  1. AcWing算法提高课
http://www.lryc.cn/news/268827.html

相关文章:

  • Mybatis Mapper XML文件-缓存(cache)
  • 电子科大软件系统架构设计——设计模式
  • ubuntu20 安装缺失的字体
  • 2023年12月27日学习记录_加入噪声
  • Java面试题86-95
  • 看完谁再说搞不定上下角标?
  • 在 Python 中使用装饰器decorator的 7 个层次
  • Vue.js项目部署至Linux服务器的详细步骤
  • Java三层架构/耦合/IOC/DI
  • [调试]stm32使用过程debug记录,持续更新ing
  • 知识付费小程序如何搭建?
  • springboot整合minio做文件存储
  • 拥抱鸿蒙 - 在展讯T606平台上的探索与实践
  • nginx源码分析-1
  • 超分之SRGAN
  • Illustrator脚本 #015 自动角线
  • 使用Vite创建React + TypeScript(pro和mobile,含完整的空项目结构资源可供下载)
  • 第一次记录QPSK,BSPK,MPSK,QAM—MATLAB实现
  • 每周一算法:区间覆盖
  • im6ull学习总结(二)Framebuffer 应用编程
  • 数据仓库 基本信息
  • 仓储革新:AR技术引领物流进入智慧时代
  • 软件仓库部署及应用
  • ASUS华硕ROG幻16笔记本电脑2023款GU604VI VZ VY原装出厂Windows11系统22H2
  • 可视化云监控/安防监控系统EasyCVR视频管理平台播流失败的原因(端口篇)
  • 边缘检测——PidiNet网络训练自己数据集并优化推理测试(详细图文教程)
  • SpringBoot整合Mybatis遇到的常见问题及解决方案
  • 【10】ES6:Promise 对象
  • Hive和Spark生产集群搭建(spark on doris)
  • VuePress、VuePress-theme-hope 搭建个人博客 1【快速上手】 —— 防止踩坑篇