SPFA 算法
百度百科:
http://baike.baidu.com/link?url=O0QvxbOY8SVBjrIl6nF6EvMHSslgcEIxfXSoty5SbkA7QjbWZjTWARzwTQsKKbSD5mlASljndZrqYjle_qwcmq#reference-[1]-4700690-wrap
模板:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include <deque>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define INF 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1010
#define maxv 1010
#define mod 1000000000
using namespace std; struct edge
{
int to,weight;
};
vector<edge>adjmap[maxn]; //动态邻接表
bool in_queue[maxn]; //顶点是否在队列中
int in_sum[maxn];//顶点入队次数
int dist[maxn];//源点到各点的最短路径
int path[maxn]; //存储到达i的前一个顶点
int nodesum; //顶点数
int edgesum; //边数 bool SPFA(int source)
{
deque<int>dq;
int i,j,x,to;
for(int i=;i<=nodesum;i++)
{
in_sum[i]=;
in_queue[i]=false;
dist[i]=INF;
path[i]=-;
}
dq.push_back(source);
in_sum[source]++;
dist[source]=;
in_queue[source]=true;
//初始化 完成
while(!dq.empty())
{
x=dq.front(); //取出队首元素
dq.pop_front();
in_queue[x]=false; //访问标记置0 ,同一个顶点可以访问多次,只要dist值变小
for(i=;i<adjmap[x].size();i++)
{
to=adjmap[x][i].to;
if((dist[x]<INF)&&(dist[to]>dist[x]+adjmap[x][i].weight))
{
dist[to]=dist[x]+adjmap[x][i].weight;
path[to]=x; //记录路径
if(!in_queue[to])
{
in_queue[to]=true;
in_sum[to]++; //访问次数加1
if(in_sum[to]==nodesum) return false; //访问次数等于 顶点数 存在负权回路
if(!dq.empty())
{
if(dist[to]>dist[dq.front()]) dq.push_back(to); //大的加入 队尾
else dq.push_front(to); //小的加入队首
}
else dq.push_back(to); //队列为空加入队尾
}
}
}
}
} void Prit_Path(int x)
{
stack<int>s;
int w=x;
while(path[w]!=-) //用栈保存路径
{
s.push(w);
w=path[w];
}
cout<<"顶点1到顶点"<<x<<"最短路径长度为:"<<dist[x]<<endl;
cout<<"所经过的路径为:1 ";
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
int main()
{
freopen("a.txt","r",stdin);
int i,s,e,w;
edge temp;
// cout<<"输入顶点数和边数:";
cin>>nodesum>>edgesum;
for(int i=;i<=nodesum;i++) adjmap[i].clear();
for(int i=;i<=edgesum;i++)
{
// cout<<"输入第"<<i<<"条边的起点,终点还有对应的权值:";
cin>>s>>e>>w;
temp.to=e;
temp.weight=w;
adjmap[s].push_back(temp);
}
if(SPFA())
{
for(int i=;i<=nodesum;i++) Prit_Path(i);
}
else cout<<"图中存在负权回路"<<endl;
return ;
}
SPFA 算法的更多相关文章
- 最短路径问题的Dijkstra和SPFA算法总结
Dijkstra算法: 解决带非负权重图的单元最短路径问题.时间复杂度为O(V*V+E) 算法精髓:维持一组节点集合S,从源节点到该集合中的点的最短路径已被找到,算法重复从剩余的节点集V-S中选择最短 ...
- [知识点]SPFA算法
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vx93.html 1.前言 ...
- SPFA算法
SPFA算法 一.算法简介 SPFA(Shortest Path Faster Algorithm)算法是求单源最短路径的一种算法,它是Bellman-ford的队列优化,它是一种十分高效的最短路算法 ...
- SPFA算法学习笔记
一.理论准备 为了学习网络流,先水一道spfa. SPFA算法是1994年西南交通大学段凡丁提出,只要最短路径存在,SPFA算法必定能求出最小值,SPFA对Bellman-Ford算法优化的关键之处在 ...
- 用scheme语言实现SPFA算法(单源最短路)
最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...
- SPFA算法心得
SPFA算法是改进后的Bellman-Ford算法,只是速度更快,而且作为一个算法,它更容易理解和编写,甚至比Dijkstra和B-F更易读(当然,Floyd是另一回事了,再也没有比Floyd还好写的 ...
- 最短路径--SPFA 算法
适用范围:给定的图存在负权边,这时类似Dijkstra等算法便没有了用武之地,而Bellman-Ford算法的复杂度又过高,SPFA算法便派上用场了. 我们约定有向加权图G不存在负权回路,即最短路径一 ...
- Bellman-Ford & SPFA 算法——求解单源点最短路径问题
Bellman-Ford算法与另一个非常著名的Dijkstra算法一样,用于求解单源点最短路径问题.Bellman-ford算法除了可求解边权均非负的问题外,还可以解决存在负权边的问题(意义是什么,好 ...
- UVA 10000 Longest Paths (SPFA算法,模板题)
题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...
- 最短路径算法之四——SPFA算法
SPAF算法 求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm,该算法是西南交通大学段凡丁于1994年发表的. 它可以在O(kE)的时间复杂度内求出源点 ...
随机推荐
- Android开发学习——ButterKnife使用
为了码代码的效率,我们有了ButterKnife;其基本使用如下步骤: 1.在Android Studio的Setting中,下载plugin 2.在整个Project的build.gradle中添加 ...
- 窗体WINFORM
窗体的事件:删除事件:先将事件页面里面的挂好的事件删除,再删后台代码里面的事件 Panel是一个容器 1.Label -- 文本显示工具Text:显示的文字取值.赋值:lable1.Text 2.Te ...
- AJPFX编写cmd界面下一键编译、执行java代码的bat脚本
此脚本适合刚接触java的同学,在cmd界面下用jc取代 复杂的 javac *.java + java main使用说明:把脚本内容复制到txt文本中,修改后缀名为.bat,运行一次即可完成配置, ...
- SQLite busy handler
SQLite doesn't support high concurrency. In case of a lot of concurrent access from multi-process or ...
- flex布局(主要分清楚容器和条目)
设置在容器上面的属性:flex-direction.flex-wrap.flex-flow.justify-content.align-items.align-content1.flex-direct ...
- github修改仓库项目的语言类型
github是 采用Linguist来自动识别你的代码应该归为哪一类. 解决方法: 我们可以在仓库的根目录下添加.gitattributes文件: ## 使用 `.gitattributes` 配置文 ...
- KMP中next数组的理解与应用
理解 1.next数组一直往前走 next数组一直往前走,得到的所有前缀也是当前主串的后缀,当然了,也是当前主串的前缀. 2.周期性字符串 1.周期性字符串$\Leftrightarrow n \,\ ...
- QStandardItemModel
QString("%1").arg(g_PrjMg.m_Param.stRunParaSet.wWDTTimer) ///站号参数 model = new QStandardIte ...
- myeclipse出现Failed to load JavaHL Library.
eclipse启动出现如图的状况: 解决方法: Window-Preferences-Team-SVN,在SVN接口的下拉框可以看到,默认选择的是JavaHL(JNI) Not Available,手 ...
- 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置
出题:不同大小烙饼的排序问题:对于N块大小不一的烙饼,上下累在一起,由于一只手托着所有的饼,所以仅有一只手可以翻转饼(假设手足够大可以翻转任意块数的 饼),规定所有的大饼都出现在小饼的下面则说明已经排 ...