当前位置:网站首页>spfa AcWing 851. SPFA finding the shortest path
spfa AcWing 851. SPFA finding the shortest path
2022-07-02 12:42:00 【T_ Y_ F666】
spfa AcWing 851. spfa Find the shortest path
Original link
851. spfa Find the shortest path
Algorithm tags
shortest path spfa
Ideas
Find the shortest path algorithm summary 
This picture is taken from this video
spfa The algorithm is right Bellman_ford Optimization of algorithm
Bellman_ford The algorithm will traverse all the edges , But there are a lot of edge traversals, which actually makes no sense , We only need to traverse the edges connected by the points whose distance to the source becomes smaller , Only when the precursor node of a point is updated , This node will be updated ; So with this in mind , We will create a queue, each time we join the node whose distance is updated .
The figure is extracted from the solution to the problem
Code
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100005;
int h[N], e[N], ne[N], w[N], idx;
int dis[N];
bool st[N];
int n,m;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
void add(int a, int b, int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
int sp(){
memset(dis, 0x3f, sizeof dis);
dis[0]=1;
queue<int> q;
q.push(1);
st[1]=true;
while(q.size()){
int t=q.front();
q.pop();
// After being taken out of the queue, the node st Marked as false, After the delegate, if the node is updated, it can join the queue again
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(dis[j]>dis[t]+w[i]){
dis[j]=dis[t]+w[i];
if(!st[j]){// The node that has been added to the queue , No need to join the queue again , Even if an update occurs, only the value can be updated , Repeated addition reduces efficiency
q.push(j);
st[j]=true;
}
}
}
}
return dis[n];
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(h, -1, sizeof h);
n=read(), m=read();
while(m--){
int x=read(), y=read(), z=read();
add(x, y, z);
}
if(sp()==0x3f3f3f3f){
puts("impossible");
}else{
printf("%lld\n", sp());
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- Calculate the maximum path sum of binary tree
- 高性能纠删码编码
- What data types does redis have and their application scenarios
- Drools terminates the execution of other rules after executing one rule
- 通过反射执行任意类的任意方法
- 考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
- JS6day(DOM结点的查找、增加、删除。实例化时间,时间戳,时间戳的案例,重绘和回流)
- 浏览器存储方案
- How to write a pleasing English mathematical paper
- Enhance network security of kubernetes with cilium
猜你喜欢

深拷贝 事件总线

趣味 面试题

高性能纠删码编码
![[ybtoj advanced training guide] similar string [string] [simulation]](/img/eb/acfefc7f85018fe9365d13502e2b0a.jpg)
[ybtoj advanced training guide] similar string [string] [simulation]

浏览器node事件循环

China traffic sign detection data set

Lekao.com: experience sharing of junior economists and previous candidates in customs clearance

Anti shake throttle

Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)

Simple use of drools decision table
随机推荐
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
分布式机器学习框架与高维实时推荐系统
Intel internal instructions - AVX and avx2 learning notes
AI mid stage technology research
JSON序列化 与 解析
高性能纠删码编码
基于STM32的OLED 屏幕驱动
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Mongodb redis differences
Drools executes the specified rule
Is the neural network (pinn) with embedded physical knowledge a pit?
AI中台技术调研
String palindrome hash template question o (1) judge whether the string is palindrome
染色法判定二分图 AcWing 860. 染色法判定二分图
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
Record the range of data that MySQL update will lock
bellman-ford AcWing 853. 有边数限制的最短路
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
Simple use of drools decision table