当前位置:网站首页>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;
}
边栏推荐
- Introduction to the paper | application of machine learning in database cardinality estimation
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- Markdown基础语法
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- 《架构整洁之道》读书笔记(下)
- 《代码整洁之道》读书笔记
- golang:[]byte转string
- PHP非对称加密方法私钥及公钥加密解密的方法
- 《重构:改善既有代码的设计》读书笔记(上)
- Digital scroll strip animation
猜你喜欢

Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

Imitation Jingdong magnifying glass effect (pink teacher version)

全志A33使用主线U-Boot

End-to-End Object Detection with Transformers(DETR)论文阅读与理解

zabbix5客户端安装和配置

高级性能测试系列《24. 通过jdbc执行sql脚本》

Thread application instance

新手必看,點擊兩個按鈕切換至不同的內容

论文导读 | 关于将预训练语言模型作为知识库的分析与批评
随机推荐
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Digital scroll strip animation
2022.7.1-----leetcode.241
MySQL高级(进阶)SQL语句
PyTorch函数中的__call__和forward函数
《重构:改善既有代码的设计》读书笔记(上)
Codeworks 5 questions per day (1700 average) - day 4
Learn the knowledge points of eight part essay ~ ~ 1
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
全志A33使用主线U-Boot
《代码整洁之道》读书笔记
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
MySQL表历史数据清理总结
思考变量引起的巨大变化
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
搭建哨兵模式reids、redis从节点脱离哨兵集群
Compile oglpg-9th-edition source code with clion
Usage of ieda refactor
论文导读 | 机器学习在数据库基数估计中的应用
冒泡排序数组