求最短路是图论中最基础的算法,最短路算法挺多,本文介绍SPFA算法。 关于其他最短路算法,请看我另一篇博客最短路算法详解

链式前向星概念

简单的说,就是存储图的一个数据结构。它是按照边来存图,而邻接矩阵是按点来存图,故链式前向星又叫边集数组

为何用链式前向星

当图的边数不多,而节点数很多(稠密图)的时候,如果我们仍然用邻接矩阵来存的话,内存占用可能会很大,而这种情况在ACM竞赛中又是很常见的,此时链式前向星就显得尤为重要。

链式前向星详解

主要涉及到两个数组,一个是head[MAXE]数组,另一个是edge[MAXE]数组:

// edge数组是一个边集数组,存放一条边的信息
// to --- 该条边的终点
// next --- 下一条要访问的边(存的是edge数组的下标).
// 即:访问完了edge[i],下一条要访问的就是edge[edge[i].next],
// 如果next为0,表示now这个节点作为起点的边已经全部访问完.(下一步:Q.front())
// w --- 该条边的权值
struct Node {
int to,next,w;
};
Node edge[MAXE]; // idx --- edge数组的下标
// head[i] --- 表示以i节点为起点的所有出边在edge数组中的起始存储位置为head[i].
// (如果head[i]为0,表示结点i没有出边)
int idx,head[MAXV];

理解了这两个数组,那么链式前向星也就理解了。其实链式前向星主要还是理解head数组和edge数组中的next这两个东西是怎么相互作用的,简单的说,就是head数组指导next,next指导edge的下一个下标。即:head[i]存储的是节点i作为起始节点的出边在edge数组中的起始存储位置,next引导节点i的下一条出边在edge数组中的存储位置。

怎么实现图的存储的呢?

这张图看懂了,链式前向星也就搞懂了。

链式前向星代码实现

//Memory   Time
// 2556K 362MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAXV 10010
#define MAXE 50010
#define LL long long
using namespace std;
int T,n,m,u,v,w;
int now,home,goal;
bool vis[MAXV];
LL dis[MAXV];
namespace Adj
{
// edge数组是一个边集数组,存放一条边的信息
// to --- 该条边的终点
// next --- 下一条要访问的边(存的是edge数组的下标).
// 即:访问完了edge[i],下一条要访问的就是edge[edge[i].next],
// 如果next为0,表示now这个节点作为起点的边已经全部访问完.(下一步:Q.front())
// w --- 该条边的权值
struct Node
{
int to,next,w;
};
Node edge[MAXE]; // idx --- edge数组的下标
// head[i] --- 表示以i节点为起点的所有出边在edge数组中的起始存储位置为head[i].(如果head[i]为0,表示结点i没有出边)
int idx,head[MAXV];
// 初始化
void init()
{
idx=;
memset(head,,sizeof(head));
} // 加边函数
void addEdge(int u,int v,int w) // 起点,终点,权值
{
edge[idx].to=v; // 该边的终点
edge[idx].w=w; // 权值
edge[idx].next=head[u]; // (指向head[u]后,head[u]又指向了自己)
head[u]=idx; // 以u结点为起点的边在edge数组中存储的下标
idx++;
}
}
using namespace Adj;
void visit(int sta)
{
for(int i=;i<=n;i++)
{
vis[i]=;
dis[i]=LLONG_MAX;
}
// 起点进队
queue<int>Q;
Q.push(sta);
vis[sta]=;
dis[sta]=;
while(!Q.empty())
{
int now=Q.front();
Q.pop();
// 在spfa中这儿需要改为0,因为每个节点需要重复进队
vis[now]=;
//取出now结点在edge中的起始存储下标(当i=0,即edge[i].next为0,说明以now节点为起始点的边全部访问完)
for(int i=head[now];i;i=edge[i].next)
{
int son=edge[i].to;
printf("%d --> %d , weight = %d\n",now,edge[i].to,edge[i].w);
if(!vis[son])
{
Q.push(son); // 子节点未访问过
vis[son]=; // 标记已访问
}
}
}
} int main()
{
while()
{
Adj::init();
scanf("%d",&n);
scanf("%d",&m);
while(m--) // 输入m条边
{
int s,e,w; // 起点 终点 权值
scanf("%d %d %d",&s,&e,&w);
addEdge(s,e,w); //若是无向图,反过来再加一次
}
int start_point; //访问的起点
scanf("%d",&start_point);
visit(start_point);
}
return ;
}

spfa概念

SPFA算法是求单源最短路径的一种算法,在Bellman-ford算法的基础上加上一个队列优化,减少了冗余的松弛操作,是一种高效的最短路算法。

SPFA的运用和分析
运用:

  1. 求单源最短路;
  2. 判断负环(某个点进队的次数超过了v次,则存在负环)

分析:

  1. 平均时间复杂度:O(kE),k<=2
  2. 最差时间复杂度:O(VE) (出题人可能设计卡spfa时间复杂度的数据)

SPFA代码实现

//Memory   Time
// 2556K 362MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#inlude<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAXV 10010
#define MAXE 50010
#define LL long long
using namespace std;
int T,n,m,u,v,w;
int now,home,goal;
bool vis[MAXV];
LL dis[MAXV];
namespace Adj
{
// edge数组是一个边集数组,存放一条边的信息
// to --- 该条边的终点
// next --- 下一条要访问的边(存的是edge数组的下标).即:访问完了edge[i],下一条要访问的就是edge[edge[i].next],如果next为0,表示now这个节点作为起点的边已经全部访问完.(下一步:Q.front())
// w --- 该条边的权值
struct Node
{
int to,next,w;
};
Node edge[MAXE]; // idx --- edge数组的下标
// head[i] --- 表示以i节点为起点的所有出边在edge数组中的起始存储位置为head[i].(如果head[i]为0,表示结点i没有出边)
int idx,head[MAXV];
// 初始化
void init()
{
idx=;
memset(head,,sizeof(head));
} // 加边函数
void addEdge(int u,int v,int w) // 起点,终点,权值
{
edge[idx].to=v; // 该边的终点
edge[idx].w=w; // 权值
edge[idx].next=head[u]; // (指向head[u]后,head[u]又指向了自己)
head[u]=idx; // 以u结点为起点的边在edge数组中存储的下标
idx++;
}
}
using namespace Adj;
void visit(int sta)
{
for(int i=;i<=n;i++)
{
vis[i]=;
dis[i]=LLONG_MAX;
}
// 起点进队
queue<int>Q;
Q.push(sta);
vis[sta]=;
dis[sta]=;
while(!Q.empty())
{
int now=Q.front();
Q.pop();
vis[now]=; // 在spfa中这儿需要改为0,因为每个节点需要重复进队
for(int i=head[now];i;i=edge[i].next) //取出now结点在edge中的起始存储下标(当i=0,即edge[i].next为0,说明以now节点为起始点的边全部访问完)
{
int w=edge[i].w;
int son=edge[i].to;
printf("%d --> %d , weight = %d\n",now,edge[i].to,edge[i].w);
if(dis[now]+w<dis[son]) // 松弛操作
{
dis[son]=dis[now]+w;
if(!vis[son])
{
Q.push(son); // 子节点未访问过
vis[son]=; // 标记已访问
}
} }
}
puts("/*************************************** END ******************************************/");
for(int i=;i<=n;++i)
{
printf("%d --> %d shortest distance is %d\n",sta,i,dis[i]);
} } int main()
{
while()
{
Adj::init();
scanf("%d",&n);
scanf("%d",&m);
for(int i=;i<=m;++i) // 输入m条边
{
int s,e,w; // 起点 终点 权值
scanf("%d %d %d",&s,&e,&w);
addEdge(s,e,w); //若是无向图,反过来再加一次
}
int start_point; //访问的起点
scanf("%d",&start_point);
visit(start_point);
}
return ;
} /*
5 6
1 2 5
1 3 9
1 4 1
3 5 2
4 3 3
4 5 7
1
*/

SPFA + 链式前向星(详解)的更多相关文章

  1. UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

  2. UESTC 30.最短路-最短路(Floyd or Spfa(链式前向星存图))

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

  3. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

  4. SPFA+链式前向星

    板子 #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf=2<<3 ...

  5. spfa+链式前向星模板

    #include<bits/stdc++.h> #define inf 1<<30 using namespace std; struct Edge{ int nex,to,w ...

  6. POJ 3169 Layout(差分约束+链式前向星+SPFA)

    描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  7. 链式前向星+SPFA

    今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...

  8. 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板

    一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...

  9. 【模板】链式前向星+spfa

    洛谷传送门--分糖果 博客--链式前向星 团队中一道题,数据很大,只能用链式前向星存储,spfa求单源最短路. 可做模板. #include <cstdio> #include <q ...

随机推荐

  1. go中的事件对象time.Duration

    const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Sec ...

  2. [Java] Spring boot 的 SrpingSecurity 框架搭建

    参考: SrpingSecurity]之一:框架搭建https://blog.csdn.net/qq_28296925/article/details/82021092 [SpringSecurity ...

  3. Cogs 513. 八(容斥原理)

    八 ★☆ 输入文件:eight.in 输出文件:eight.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 八是个很有趣的数字啊.八=发,八八=爸爸,88=拜拜.当然最有趣的 ...

  4. 用户生命周期(User Lifetime)

    什么是用户生命周期? 用户生命周期是从用户开始接触产品到离开产品的整个过程.用户生命周期可分为:引入期.成长期.成熟期.休眠期.流失期.对应的是用户对产品不同的参与程度. 用户生命周期有什么用? 按照 ...

  5. COCI 2015、2016 1st round 题解(官方)

    官方题解: 官方代码: Code-KARTE: #include <cstdio> #include <iostream> #include <cstring> u ...

  6. 内置函数— — eval、exec、compile

    字符串类型代码:eval.exec.compile eval()  执⾏字符串类型的代码,并返回最终结果 print(eval("2+2")) # 4 n=8 def func() ...

  7. GoCN每日新闻(2019-11-06)

    GoCN每日新闻(2019-11-06) GoCN每日新闻(2019-11-06) 1. 使用构建标签分离你的测试文件 https://mickey.dev/posts/go-build-tags-t ...

  8. exit命令

    exit命令用于退出当前shell,在shell脚本中可以终止当前脚本执行. 常用参数格式:exit n退出.设置退出码为n.(Cause the shell to exit with a statu ...

  9. 回滚事件只是让原数据看起来不变,但是id还是会自增吗?

    回滚事件只是让原数据看起来不变,但是id还是会自增对吗? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  10. CAP原则 (阿里)

    CAP原则又称CAP定理,指的是在一个分布式系统中,一致性(Consistency).可用性(Availability).分区容错性(Partition tolerance).CAP 原则指的是,这三 ...