当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

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

Yolov3 trains its own data set to generate train txt

IEDA refactor的用法

xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机

Markdown基础语法
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)

《重构:改善既有代码的设计》读书笔记(上)

Novice must see, click two buttons to switch to different content

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept
随机推荐
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
Reading notes of code neatness
Compile oglpg-9th-edition source code with clion
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
C file input operation
C文件输入操作
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Qpropertyanimation use and toast case list in QT
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
多态的理解以及作用
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
数据降维——因子分析
开发固定资产管理系统,开发固定资产管理系统用什么语音
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
【测试开发】一文带你了解什么是软件测试
Usage of ieda refactor
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Develop fixed asset management system, what voice is used to develop fixed asset management system