Shortest Path Faster Algorithm(SPFA)模板

 SPFA算法是求解单源最短路径问题的一种算法,由理查德·贝尔曼(Richard Bellman) 和 莱斯特·福特 创立的。有时候这种算法也被称为 Moore-Bellman-Ford 算法,因为 Edward F. Moore 也为这个算法的发展做出了贡献。它的原理是对图进行V-1次松弛操作,得到所有可能的最短路径。其优于迪科斯彻算法的方面是边的权值可以为负数、实现简单,缺点是时间复杂度过高,高达 O(VE)。但算法可以进行若干种优化,提高了效率。

#include<cstring>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e6+100;
int n,m,distanc[maxn],vis[maxn],s,t;

struct Edge{
	int from,to,dis;
	Edge(int f,int t,int d):from(f),to(t),dis(d){};
};

vector<Edge>G[maxn];
queue<int>que;

int spfa(int s){
	for(int i=1;i<=n;i++) distanc[i]=inf;
	distanc[s]=0;
	memset(vis,0,sizeof(vis));
	que.push(s);
	//1.一些初始化的操作 
	while(!que.empty()){
		int u=que.front();//u是队头元素 
		vis[u]=0;
		que.pop();
		for(int i=0;i<G[u].size();i++){
			Edge e=G[u][i];//e是一个结构体 
			if(distanc[e.to]>distanc[e.from]+e.dis){
				distanc[e.to]=distanc[e.from]+e.dis;
				if(!vis[e.to]){//如果它没有入队 
					vis[e.to]=1;//把他入队 
					que.push(e.to);
				}
			}
		}
	}
	return distanc[t];
}

int main(){
	cin>>n>>m>>s>>t;
	for(int i=0;i<m;i++){
		int from,to,dist;
		cin>>from>>to>>dist;
		G[from].push_back(Edge(from,to,dist));
		G[to].push_back(Edge(to,from,dist));
	}
	printf("%d\n",spfa(s));
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++中的SPFAShortest Path Faster Algorithm算法的基本模板: ```cpp #include <iostream> #include <vector> #include <queue> #include <limits> const int INF = std::numeric_limits<int>::max(); // 无穷大值 struct Edge { int to; int weight; }; void addEdge(std::vector<std::vector<Edge>>& graph, int from, int to, int weight) { graph[from].push_back({to, weight}); } void spfa(std::vector<std::vector<Edge>>& graph, std::vector<int>& dist, int start) { int n = graph.size(); dist[start] = 0; std::vector<bool> inQueue(n, false); std::queue<int> q; q.push(start); inQueue[start] = true; while (!q.empty()) { int node = q.front(); q.pop(); inQueue[node] = false; for (const auto& edge : graph[node]) { int to = edge.to; int weight = edge.weight; if (dist[node] + weight < dist[to]) { dist[to] = dist[node] + weight; if (!inQueue[to]) { q.push(to); inQueue[to] = true; } } } } } int main() { int n = 6; // 节点数量 int start = 0; // 起始节点 std::vector<std::vector<Edge>> graph(n); std::vector<int> dist(n, INF); // 添加边 addEdge(graph, 0, 1, 2); addEdge(graph, 0, 2, 5); addEdge(graph, 1, 2, 1); addEdge(graph, 1, 3, 6); addEdge(graph, 2, 3, 2); addEdge(graph, 2, 4, 3); addEdge(graph, 3, 4, 1); addEdge(graph, 3, 5, 4); addEdge(graph, 4, 5, 2); spfa(graph, dist, start); std::cout << "最短路径长度:" << std::endl; for (int i = 0; i < n; ++i) { std::cout << "从起点 " << start << " 到节点 " << i << ": " << dist[i] << std::endl; } return 0; } ``` 在上面的模板中,我们首先定义了一个`Edge`结构体,表示图的边。然后,我们实现了`addEdge`函数来添加边到图中。接下来,我们定义了`spfa`函数来执行SPFA算法。最后,在`main`函数中,我们创建了一个图,并调用`spfa`函数来计算最短路径。最终,我们打印出从起点到每个节点的最短路径长度。 请注意,这只是SPFA算法的基本模板,具体的实现和使用可能会根据实际情况有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值