最短路 spfa 算法 && 链式前向星存图
推荐博客 https://i.cnblogs.com/EditPosts.aspx?opt=1
http://blog.csdn.net/mcdonnell_douglas/article/details/54379641
spfa 自行百度 说的很详细
spfa 有很多实现的方法 dfs 队列 栈 都可以 时间复杂度也不稳定 不过一般情况下要比bellman快得多
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <ctime>
#include <vector>
using namespace std;
const int maxn= 1e3+;
const int maxm= 1e3+;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int n,m,s; //n m s 分别表示 点数-标号从1开始 边数-标号从0开始 起点
struct edge
{
int to,w;
};
int d[maxn]; //d[i]表示 i 点到源点 s 的最短距离
int p[maxn]; //p[i]记录最短路到达 i 之前的节点
int visit[maxn]; // 标记是否已进队
int cnt[maxn];
vector<edge> v[maxn];
int spfa(int x)
{
queue<int> q;
memset(visit,,sizeof(visit));
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++)
d[i]=inf;
d[x]=;
visit[x]=;
q.push(x);
while(!q.empty())
{
int u=q.front();q.pop();
visit[u]=;
for(int i=;i<v[u].size();i++)
{
edge &e=v[u][i];
if(d[u]<inf&&d[u]+e.w<d[e.to])
{
d[e.to]=d[u]+e.w;
p[e.to]=u;
if(!visit[e.to])
{
q.push(e.to);
visit[e.to]=;
if(++cnt[e.to]>n)
return ;
}
}
}
}
return ;
}
void Print_Path(int x)
{
while(x!=p[x]) //逆序输出 正序的话用栈处理一下就好了
{
printf("%d ",x);
x=p[x];
}
printf("%d\n",x);
}
int main()
{
while(scanf("%d %d %d",&n,&m,&s)!=EOF)
{
int x,y,z;
for(int i=;i<m;i++)
{
scanf("%d %d %d",&x,&y,&z);
edge e;
e.to=y;
e.w=z;
v[x].push_back(e);
// e.to=x; //无向图 反向建边
// v[y].push_back(e);
}
p[s]=s;
if(spfa(s)==)
for(int i=;i<=n;i++)
{
printf("%d %d\n",i,d[i]);
Print_Path(i);
}
else
printf("sorry\n");
return ;
}
}
链式前向星存图
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <ctime>
#include <vector>
using namespace std;
const int maxn= 1e3+;
const int maxm= 1e3+;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int n,m;
int first[maxn];
struct edge
{
int to,next,w;
}e[maxn];
void add(int i,int u,int v,int w)
{
e[i].to=v;
e[i].w=w;
e[i].next=first[u];
first[u]=i;
}
int main()
{
scanf("%d %d",&n,&m);
{
int u,v,w;
memset(first,-,sizeof(first));
for(int i=;i<m;i++)
{
scanf("%d %d %d",&u,&v,&w);
add(i,u,v,w);
}
for(int i=;i<=n;i++)
{
cout<<"from"<<i<<endl;
for(int j=first[i];j!=-;j=e[j].next) //遍历以j为起点的每条边
cout<<"to"<<e[j].to<<" length="<<e[j].w<<endl;
} }
}
//输入
//6 9
//1 2 2
//1 4 -1
//1 3 1
//3 4 2
//4 2 1
//3 6 3
//4 6 3
//6 5 1
//2 5 -1
//输出
//from1
//to3 length=1
//to4 length=-1
//to2 length=2
//from2
//to5 length=-1
//from3
//to6 length=3
//to4 length=2
//from4
//to6 length=3
//to2 length=1
//from5
//from6
//to5 length=1
最短路 spfa 算法 && 链式前向星存图的更多相关文章
- C++算法 链式前向星存图
这个东西恶心了我一阵子,那个什么是什么的上一个一直是背下来的,上次比赛忘了,回来有个题也要用,只能再学一遍,之前也是,不会为什么不学呢.我觉得是因为他们讲的不太容易理解,所以我自己给那些不会的人们讲一 ...
- UESTC 30.最短路-最短路(Floyd or Spfa(链式前向星存图))
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- Pants On Fire(链式前向星存图、dfs)
Pants On Fire 传送门:链接 来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...
- [板子]SPFA算法+链式前向星实现最短路及负权最短路
参考:https://blog.csdn.net/xunalove/article/details/70045815 有关SPFA的介绍就掠过了吧,不是很赞同一些博主说是国内某人最先提出来,Bellm ...
- UESTC30-最短路-Floyd最短路、spfa+链式前向星建图
最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...
- 链式前向星存树图和遍历它的两种方法【dfs、bfs】
目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...
- 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)
Dijkstra+ 链式前向星+ 优先队列 Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...
- HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】
最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...
- POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)
题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
随机推荐
- 浅谈传统语音通信和APP语音通信音频软件开发之不同点
本人在传统的语音通信公司做过手机和IP电话上的语音软件开发,也在移动互联网公司做过APP上的语音软件开发.现在带实时语音通信功能的APP有好多,主流的有微信语音.QQ电话.钉钉等,当然也包括我开发过的 ...
- java操作时间,将当前时间减一年,减一天,减一个月
在Java中操作时间的时候,常常遇到求一段时间内的某些值,或者计算一段时间之间的天数 Date date = new Date();//获取当前时间 Calendar calendar = Calen ...
- Ruby学习之对象模型
这两周工作内容较多,平时自己也有点不在状态,学的东西有点少了,趁着现在还有点状态,赶紧复习一下之前学习的Ruby吧. Ruby是我真正开始接触动态语言魅力的第一个语言,之前虽然也用过且一直用perl. ...
- vue cli搭建项目及文件引入
cli搭建方法:需安装nodejs先 1.npm install -g cnpm --registry=https://registry.npm.taobao.org //安装cnpm,用cnpm下载 ...
- Python类方法、静态方法与实例方法
静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实例化,直接在类之后使用.号运算符调用方法. 通常情况下,静态方法使用@staticmethod装饰器来声明. ...
- SpringMVC 支持使用Servlet原生API作为目标方法的参数
具体支持一下类型: * HttpServletRequest * HttpServletResponse * HttpSession * java.security.Pricipal * Locale ...
- golang 栈操作
Monk's Love for Food Our monk loves food. Hence,he took up position of a manager at Sagar,a restau ...
- SecureCRT 历史版本下载
最近在使用SecureCRT时,存在网络卡顿现象,然而.同事的SecureCRT工具却一点都不卡,我的SecureCRT是比较老的版本6,同事使用的是版本7,所以就更换下自己的SecureCRT版本. ...
- JS中call,apply,bind方法的总结
why?call,apply,bind干什么的?为什么要学这个? 一般用来指定this的环境,在没有学之前,通常会有这些问题. var a = { user: "小马扎", fn: ...
- 深入研究ES6 Generators
ES6 Generators系列: ES6 Generators基本概念 深入研究ES6 Generators ES6 Generators的异步应用 ES6 Generators并发 如果你还不知道 ...