当前位置:网站首页>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
边栏推荐
- Direct control PTZ PTZ PTZ PTZ camera debugging (c)
- Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
- JSON序列化 与 解析
- How to write a pleasing English mathematical paper
- Day12 control flow if switch while do While guessing numbers game
- OpenCV中cv2.VideoWriter_fourcc()函数和cv2.VideoWriter()函数的结合使用
- BOM DOM
- Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
- 堆 AcWing 838. 堆排序
- Mongodb redis differences
猜你喜欢
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
AI mid stage technology research
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
线性DP AcWing 897. 最长公共子序列
堆 AcWing 838. 堆排序
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
高性能纠删码编码
VLAN experiment
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime
随机推荐
Adding database driver to sqoop of cdh6
Intel 内部指令 --- AVX和AVX2学习笔记
线性DP AcWing 898. 数字三角形
Floyd AcWing 854. Floyd求最短路
JDBC 预防sql注入问题与解决方法[PreparedStatement]
Drools executes string rules or executes a rule file
String palindrome hash template question o (1) judge whether the string is palindrome
In development, why do you find someone who is paid more than you but doesn't write any code?
Leetcode - Sword finger offer 51 Reverse pairs in an array
堆 AcWing 839. 模拟堆
What data types does redis have and their application scenarios
高性能纠删码编码
When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
Embedded Software Engineer career planning
Sparkcontext: error initializing sparkcontext solution
模块化 CommonJS ES Module
Record the range of data that MySQL update will lock
接口测试面试题目,你都会了吗?
浏览器node事件循环
Direct control PTZ PTZ PTZ PTZ camera debugging (c)