最短路 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相同就选取编号最小的 题解: 树的重心定义:找到一个点,其所 ...
随机推荐
- 数据库的SQL优化
1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. (因为在条件查询条件添加索引,会直接被检索到列,会非常的快速) 2.应尽量避免在 wher ...
- 快速学习Bash
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. Shell是Linux下经典的文本互动方式,而Bash是现在最常用的一种Shell.我在这里总结了Bash ...
- csdn博客被删除联系客服恢复
前几天写了篇"如何使用shadowsocksFQ",居然被删除,我很郁闷,客服给我的回答是"影响了客户体验,网安查的严,不能带有FQ的信息出现" 我一直很郁闷, ...
- bzoj 2959: 长跑
Description 某校开展了同学们喜闻乐见的阳光长跑活动.为了能"为祖国健康工作五十年",同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上 ...
- bzoj 1855: [Scoi2010]股票交易
Description 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股票的走势,第i天的股票买入价 ...
- Swift学习第一天--面向过程
//: Playground - noun: a place where people can play import UIKit //---------------------- Hello wor ...
- bootstrap 导航栏鼠标悬停显示下拉菜单
在jsp中加入一下代码: .navbar .nav > li:hover .dropdown-menu { display: block;} 全部代码如下所示: <%@ page lang ...
- 怎么为WebStorm更换主题 修改字体样式
这篇文章主要用于帮助大家解决怎么为webstorm换theme. 首先,到选择一个自己喜欢的皮肤,Webstorm皮肤网址: http://phpstorm-themes.com/ 然后,选中你喜欢的 ...
- 房上的猫:HTML5基础
一.W3C标准 1)W3C标准不是某一个标准,而是一系列的标准的集合,一个网页主要由三部分组成,即结构(Structure),表现(Presentation)和行为(Behavior) 2)不很严谨的 ...
- 454ITS数据按barcode和primer分类程序v1.0
不知道有什么好办法可以让primer允许漏配,现在仅仅是允许错配,还是有一些没有配上,454数据有些primer漏配了一些,下一步解决这个问题 #include <cstdio> #inc ...