当前位置:网站首页>AcWing 1129. Heat wave solution (shortest path SPFA)
AcWing 1129. Heat wave solution (shortest path SPFA)
2022-07-02 19:39:00 【Mr. Qiao Da】
AcWing 1129. Heat Wave
SPFA Find the shortest path , Find the shortest single source , Is to establish a queue , Follow the path in the figure , Add the updated point that finds the shortest path to the queue , Then queue the elements in the queue in turn , Continue to traverse the graph according to the out of line elements , Finally find the shortest circuit
#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; // If you traverse to the last point
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;
}
边栏推荐
- Typescript 之 快速入门
- Refactoring: improving the design of existing code (Part 2)
- Gmapping code analysis [easy to understand]
- AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
- 数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
- AcWing 1129. 热浪 题解(最短路—spfa)
- Horizontal ultra vires and vertical ultra vires [easy to understand]
- Infix expression is converted to suffix expression (C language code + detailed explanation)
猜你喜欢
Yes, that's it!
Set up sentinel mode. Reids and redis leave the sentinel cluster from the node
Refactoring: improving the design of existing code (Part 1)
《重构:改善既有代码的设计》读书笔记(下)
Bubble sort array
《MongoDB入门教程》第03篇 MongoDB基本概念
搭建主从模式集群redis
《重构:改善既有代码的设计》读书笔记(上)
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
随机推荐
checklistbox控件用法总结
Typescript 之 快速入门
AcWing 1126. 最小花费 题解(最短路—dijkstra)
A4988 drive stepper motor "recommended collection"
Refactoring: improving the design of existing code (Part 2)
Data dimensionality reduction principal component analysis
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
Function high order curry realization
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
函数高阶-柯里化实现
安装单机redis详细教程
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
Horizontal ultra vires and vertical ultra vires [easy to understand]
高并发下如何避免产生重复数据?
AcWing 1129. 热浪 题解(最短路—spfa)
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
AcWing 1131. Saving Private Ryan (the shortest way)
2022.7.1-----leetcode. two hundred and forty-one
End-to-End Object Detection with Transformers(DETR)论文阅读与理解