当前位置:网站首页>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;
}
边栏推荐
- PXE installation "recommended collection"
- AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
- How to set priorities in C language? Elaborate on C language priorities
- LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
- Function high order curry realization
- Refactoring: improving the design of existing code (Part 1)
- IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
- Watchful pioneer world outlook Architecture - (how does a good game come from)
- 职场四象限法则:时间管理四象限与职场沟通四象限「建议收藏」
- Dictionaries
猜你喜欢

搭建主从模式集群redis

Py's interpret: a detailed introduction to interpret, installation, and case application

MySQL function

KT148A语音芯片ic的硬件设计注意事项

Usage of ieda refactor

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

字典

Quanzhi A33 uses mainline u-boot

Watchful pioneer world outlook Architecture - (how does a good game come from)

定了,就是它!
随机推荐
程序猿入门攻略(十二)——数据的存储
MySQL advanced (Advanced) SQL statement
Golang concurrent programming goroutine, channel, sync
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Chapter 7 - class foundation
450-深信服面经1
JS如何取整数
451-memcpy、memmove、memset的实现
《MongoDB入门教程》第03篇 MongoDB基本概念
定了,就是它!
嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件
c语言里怎么设立优先级,细说C语言优先级
AcWing 1129. 热浪 题解(最短路—spfa)
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
checklistbox控件用法总结
MySQL
Advanced performance test series "24. Execute SQL script through JDBC"
IEDA refactor的用法
Istio1.12:安装和快速入门
AcWing 1127. 香甜的黄油 题解(最短路—spfa)