当前位置:网站首页>spfa AcWing 851. spfa求最短路
spfa AcWing 851. spfa求最短路
2022-07-02 09:43:00 【T_Y_F666】
spfa AcWing 851. spfa求最短路
原题链接
算法标签
最短路 spfa
思路
求最短路算法汇总
该图摘自该视频
spfa算法是对Bellman_ford算法的优化
Bellman_ford算法会遍历所有的边,但是有很多的边遍历了其实没有什么意义,我们只用遍历那些到源点距离变小的点所连接的边即可,只有当一个点的前驱结点更新了,该节点才会得到更新;因此考虑到这一点,我们将创建一个队列每一次加入距离被更新的结点。
该图摘自该题解
代码
#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();
// 从队列中取出来之后该节点st被标记为false,代表之后该节点如果发生更新可再次入队
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]){// 当前已经加入队列的结点,无需再次加入队列,即便发生了更新也只用更新数值即可,重复添加降低效率
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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- Brush questions --- binary tree --2
- Initial JDBC programming
- Differences between nodes and sharding in ES cluster
- Sort---
- kubeadm join时出现错误:[ERROR Port-10250]: Port 10250 is in use [ERROR FileAvailable--etc-kubernetes-pki
- Post request body content cannot be retrieved repeatedly
- AI中台技术调研
- mysql索引和事务
- [C language] Yang Hui triangle, customize the number of lines of the triangle
猜你喜欢
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Heap (priority queue)
Test shift left and right
线性DP AcWing 897. 最长公共子序列
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
CDH6之Sqoop添加数据库驱动
Jenkins user rights management
arcgis js 4. Add pictures to x map
In development, why do you find someone who is paid more than you but doesn't write any code?
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
随机推荐
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
CDA数据分析——AARRR增长模型的介绍、使用
Leetcode122 the best time to buy and sell stocks II
mysql索引和事务
AI mid stage technology research
深拷貝 事件總線
Docker-compose配置Mysql,Redis,MongoDB
Adding database driver to sqoop of cdh6
初始JDBC 编程
2.7 binary tree, post order traversal - [FBI tree]
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
刷题---二叉树--2
China traffic sign detection data set
CDA data analysis -- Introduction and use of aarrr growth model
post请求体内容无法重复获取
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
Deep copy event bus
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
[FFH] little bear driver calling process (take calling LED light driver as an example)
BOM DOM