当前位置:网站首页>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
边栏推荐
- Oracle从入门到精通(第4版)
- BOM DOM
- BOM DOM
- In development, why do you find someone who is paid more than you but doesn't write any code?
- 模块化 CommonJS ES Module
- 线性DP AcWing 898. 数字三角形
- Input box assembly of the shutter package
- 线性DP AcWing 896. 最长上升子序列 II
- C#运算符
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
猜你喜欢
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
中国交通标志检测数据集
8 examples of using date commands
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
2.6 using recursion and stack - [tower of Hanoi problem]
What is the relationship between NFT and metauniverse? How to view the market? The future market trend of NFT
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Embedded Software Engineer career planning
随机推荐
基于STM32的OLED 屏幕驱动
Use MySQL events to regularly perform post seven world line tasks
JSON序列化 与 解析
js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
通过反射执行任意类的任意方法
Oracle从入门到精通(第4版)
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
浏览器node事件循环
arcgis js 4. Add pictures to x map
The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
Openssh remote enumeration username vulnerability (cve-2018-15473)
LeetCode—<动态规划专项>剑指 Offer 19、49、60
哈希表 AcWing 841. 字符串哈希
JS10day(api 阶段性完结,正则表达式简介,自定义属性,过滤敏感词案例,注册模块验证案例)
BOM DOM
C#运算符
浏览器存储方案
Bom Dom
spfa AcWing 851. spfa求最短路