当前位置:网站首页>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;
}
边栏推荐
- 高并发下如何避免产生重复数据?
- AcWing 343. Sorting problem solution (Floyd property realizes transitive closure)
- PHP asymmetric encryption method private key and public key encryption and decryption method
- NPOI导出Excel2007
- Codeworks round 802 (Div. 2) pure supplementary questions
- AcWing 1128. 信使 题解(最短路—Floyd)
- Mobile robot path planning: artificial potential field method [easy to understand]
- 第七章-类基础
- AcWing 1127. 香甜的黄油 题解(最短路—spfa)
- AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
猜你喜欢

搭建主从模式集群redis

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

Educational Codeforces Round 129 (Rated for Div. 2) 补题题解

Juypter notebook modify the default open folder and default browser

Set up sentinel mode. Reids and redis leave the sentinel cluster from the node

Machine learning notes - time series prediction research: monthly sales of French champagne

Build a master-slave mode cluster redis

Yes, that's it!

ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题

《重构:改善既有代码的设计》读书笔记(下)
随机推荐
Implementation of 453 ATOI function
AcWing 1125. Cattle travel problem solution (shortest path, diameter)
冒泡排序数组
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Notes de lecture sur le code propre
[ERP software] what are the dangers of the secondary development of ERP system?
AcWing 342. Road and route problem solving (shortest path, topological sorting)
Correspondence between pytoch version, CUDA version and graphics card driver version
Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
zabbix5客户端安装和配置
高并发下如何避免产生重复数据?
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
Detailed tutorial on installing stand-alone redis
Istio部署:快速上手微服务,
Bubble sort array
Quanzhi A33 uses mainline u-boot
搭建主从模式集群redis
checklistbox控件用法总结
pxe装机「建议收藏」
Getting started with typescript