当前位置:网站首页>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;
}
边栏推荐
- PHP asymmetric encryption method private key and public key encryption and decryption method
- Virtual machine initialization script, virtual machine mutual secret key free
- 450-深信服面经1
- C文件输入操作
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- 性能测试如何创造业务价值
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- Quanzhi A33 uses mainline u-boot
- Machine learning notes - time series prediction research: monthly sales of French champagne
- ORA-01455: converting column overflows integer datatype
猜你喜欢

Refactoring: improving the design of existing code (Part 1)

Imitation Jingdong magnifying glass effect (pink teacher version)

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

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

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

Thread application instance

codeforces每日5题(均1700)-第四天

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

How performance testing creates business value

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
随机推荐
《代码整洁之道》读书笔记
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
仿京东放大镜效果(pink老师版)
SIFT特征点提取「建议收藏」
Golang concurrent programming goroutine, channel, sync
Thread application instance
全志A33使用主线U-Boot
移动机器人路径规划:人工势场法[通俗易懂]
Codeworks 5 questions per day (1700 average) - day 4
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
PHP asymmetric encryption method private key and public key encryption and decryption method
Date tool class (updated from time to time)
Machine learning notes - time series prediction research: monthly sales of French champagne
452-strcpy、strcat、strcmp、strstr、strchr的实现
Preprocessing and preprocessing macros
A4988 drive stepper motor "recommended collection"
Notes de lecture sur le code propre
4274. Suffix expression - binary expression tree
Watchful pioneer world outlook Architecture - (how does a good game come from)