当前位置:网站首页>AcWing 1129. 热浪 题解(最短路—spfa)
AcWing 1129. 热浪 题解(最短路—spfa)
2022-07-02 18:27:00 【乔大先生】
AcWing 1129. 热浪
SPFA求最短路的,找到单源最短路,就是建立一个队列,按照图中的路径遍历,将更新过找到最短路径的点加入队列中,然后将队列中的元素依次出队,按照出队元素在图中继续遍历,最终找到最短路
#include<bits/stdc++.h>
using namespace std;
const int N = 7000;
int e[N * 2], ne[N * 2], h[N], w[N * 2], idx;
int T, C, S, E;
int dist[N];
bool st[N];
int q[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(t == 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;
st[j] = true;
if(j == N) tt = 0;
}
}
}
}
}
int main()
{
cin>>T>>C>>S>>E;
memset(h, -1, sizeof h);
for(int i = 0; i < C; i ++ ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
}
spfa();
cout<<dist[E]<<endl;
return 0;
}
边栏推荐
- Yunna | why use the fixed asset management system and how to enable it
- Thread application instance
- 第七章-类基础
- Digital scroll strip animation
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- 【ERP软件】ERP体系二次开发有哪些危险?
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- Learn the knowledge points of eight part essay ~ ~ 1
- [paper reading] Ca net: leveraging contextual features for lung cancer prediction
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
猜你喜欢
Excel finds the same value in a column, deletes the row or replaces it with a blank value
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
PHP parser badminton reservation applet development requires online system
In pytorch function__ call__ And forward functions
程序猿入门攻略(十二)——数据的存储
Yunna | why use the fixed asset management system and how to enable it
全志A33使用主线U-Boot
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
PyTorch函数中的__call__和forward函数
随机推荐
Digital scroll strip animation
Mobile robot path planning: artificial potential field method [easy to understand]
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
SIFT feature point extraction "suggestions collection"
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
搭建主从模式集群redis
论文导读 | 机器学习在数据库基数估计中的应用
虚拟机初始化脚本, 虚拟机相互免秘钥
Yolov3 trains its own data set to generate train txt
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
【测试开发】一文带你了解什么是软件测试
[pytorch learning notes] tensor
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
Quanzhi A33 uses mainline u-boot
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
安装单机redis详细教程
Golang:[]byte to string
MySQL
数据降维——因子分析