当前位置:网站首页>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;
}
边栏推荐
- MySQL advanced (Advanced) SQL statement
- Cuckoo filter
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- Notes de lecture sur le code propre
- 简书自动阅读
- Quanzhi A33 uses mainline u-boot
- Golang concurrent programming goroutine, channel, sync
- R语言使用econocharts包创建微观经济或宏观经济图、indifference函数可视化无差异曲线(indifference curve)
- The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
- Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
猜你喜欢
Watchful pioneer world outlook Architecture - (how does a good game come from)
SQLite 3.39.0 发布,支持右外连接和全外连接
Set up sentinel mode. Reids and redis leave the sentinel cluster from the node
高并发下如何避免产生重复数据?
搭建主从模式集群redis
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
PHP parser badminton reservation applet development requires online system
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Usage of ieda refactor
随机推荐
Pytorch版本、CUDA版本与显卡驱动版本的对应关系
KS004 基于SSH通讯录系统设计与实现
A4988 drive stepper motor "recommended collection"
高并发下如何避免产生重复数据?
Cuckoo filter
AcWing 342. Road and route problem solving (shortest path, topological sorting)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
AcWing 1137. Select the best line solution (the shortest circuit)
开始练习书法
《重构:改善既有代码的设计》读书笔记(上)
Detailed tutorial on installing stand-alone redis
JS如何取整数
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
mysql备份后缀是什么_mysql备份还原
AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
Think about the huge changes caused by variables
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Qpropertyanimation use and toast case list in QT
VBScript详解(一)
Advanced performance test series "24. Execute SQL script through JDBC"