推荐博客  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 算法 && 链式前向星存图的更多相关文章

  1. C++算法 链式前向星存图

    这个东西恶心了我一阵子,那个什么是什么的上一个一直是背下来的,上次比赛忘了,回来有个题也要用,只能再学一遍,之前也是,不会为什么不学呢.我觉得是因为他们讲的不太容易理解,所以我自己给那些不会的人们讲一 ...

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

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

  3. Pants On Fire(链式前向星存图、dfs)

    Pants On Fire 传送门:链接  来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...

  4. [板子]SPFA算法+链式前向星实现最短路及负权最短路

    参考:https://blog.csdn.net/xunalove/article/details/70045815 有关SPFA的介绍就掠过了吧,不是很赞同一些博主说是国内某人最先提出来,Bellm ...

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

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

  6. 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...

  7. 【最短路】Dijkstra+ 链式前向星+ 堆优化(优先队列)

    Dijkstra+ 链式前向星+ 优先队列   Dijkstra算法 Dijkstra最短路算法,个人理解其本质就是一种广度优先搜索.先将所有点的最短距离Dis[ ]都刷新成∞(涂成黑色),然后从起点 ...

  8. HDU 2544最短路 【dijkstra 链式前向星+优先队列优化】

    最开始学最短路的时候只会用map二维数组存图,那个时候还不知道这就是矩阵存图,也不懂得效率怎么样 经过几个月的历练再回头看最短路的题, 发现图可以用链式前向星来存, 链式前向星的效率是比较高的.对于查 ...

  9. POJ 1655 Balancing Act ( 树的重心板子题,链式前向星建图)

    题意: 给你一个由n个节点n-1条边构成的一棵树,你需要输出树的重心是那个节点,以及重心删除后得到的最大子树的节点个数size,如果size相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...

随机推荐

  1. iOS 轮播中遇到的问题(暂停、重新启动)

    一. 轮播的优化或者用Collection来实现 二.Timer  问题 我们可以这样来使用一个Timer [NSTimer scheduledTimerWithTimeInterval:1.0 ta ...

  2. 【1】ArcGIS API for JavaScript 4.5/4.6 本地部署

    惭愧,和我的学弟比起来,我所开始接触前端开发,ArcGIS API for JavaScript的时间和深度远远不及于他. 一年之尾,亦是一年之始,我也将正式开始我的博客生涯.本人在校学习并且做项目, ...

  3. [数据结构]C语言队列的实现

    我个人把链表.队列.栈分为一类,然后图.树分为一类.(串不考虑),分类的理由就是每一类有规律可循,即你能通过修改极少数的代码把链表变成队列.栈.(这里我们不考虑其他诸如设计模式等因素),因此本贴在讲完 ...

  4. date 命令详解

    date - print or set the system date and time Display the current time in the given FORMAT, or set th ...

  5. Python-String字符串的相关方法

  6. CKEditor 集成CKFinder集成

    lCKEditor原名FckEditor,著名的HTML编辑器,可以在线编辑HTML内容,演示一下.打开.自己人用CKEditor,网友用UBBEditor. l配置参考文档,主要将ckeditor中 ...

  7. Linux下防火墙配置

    查看防火墙的状态:/etc/init.d/iptables  status  或  service  iptables  status 1) 临时生效,重启后复原 开启: service  iptab ...

  8. 尤克里里 ukulele 单板 非kaka tom uma

    本店冲人气优惠,不搞倒闭之类的事 23寸尤克里里 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 配件选购40元全套(加棉琴包.金属变调夹. ...

  9. NestedScrollingParent, NestedScrollingChild 详解

    之前听同事提起过 NestedScrollingView,但是一直没有时间去了解,最近一段时间比较空,才开始去了解.先点开,看 NestedScrollingView 源码: public class ...

  10. 豹哥嵌入式讲堂:ARM开发之文件详解(3)- project文件

    大家好,我是豹哥,猎豹的豹,犀利哥的哥.今天豹哥给大家讲的是嵌入式开发里的project文件. 前面两节课里,豹哥分别给大家介绍了嵌入式开发中的两种典型input文件:source文件.linker文 ...