单源最短路径的Dijkstra算法.pdf
《单源最短路径的Dijkstra算法.pdf》由会员分享,可在线阅读,更多相关《单源最短路径的Dijkstra算法.pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、单源最短路径的Dijkstra算法:问题描述:给定一个带权有向图G=(V,E),其中每条边的权是非负实数。另外,还给定 V 中的一个顶点,称为源。现在要计算从源到所有其他各顶点的最短路长度。这里路的长度是指路上各边权之和。这个问题通常称为单源最短路径问题。算法描述:Dijkstra 算法是解单源最短路径的一个贪心算法。基本思想是:设置顶点集合S并不断地做贪心选择来扩充这个集合。一个顶点属于 S 当且仅当从源到该顶点的最短路径长度已知。初始时,S 中仅含有源。设u 是 G 的某一个顶点,把从源到 u 且中间只经过 S 中顶点的路称为从源到u 的特殊路径,并用数组dist 记录当前每个顶点所对应的
2、最短特殊路径长度。Dijkstra 算法每次从 V-S 中取出具有最短特殊路长度的顶点u,将 u 添加到 S 中,同时对数组 dist 做必要的修改。一旦 S 包含了所有 V 中顶点,dist 就记录了从源到所有其他顶点之间的最短路径长度。源代码:#include#defineMAX 1000#defineLEN 100 int k=0,bLEN;using namespacestd;/-数据声明-/cij表示边(i,j)的权/disti表示当前从源到顶点i 的最短特殊路径长度/previ记录从源到顶点i 的最短路径上的i 的前一个顶点/-void Dijkstra(int n,int v,i
3、nt dist,int prev,int c LEN)bool sLEN;/判断是否已存入该点到S 集合中for (int i=1;i=n;i+)dist i=cvi;si=false;/初始都未用过该点if(dist i=MAX)prev i=0;/表示 v 到 i 前一顶点不存在elseprev i=v;dist v=0;sv=true;for (int i=1;i n;i+)文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L
4、5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5
5、W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I
6、9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C
7、3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9
8、L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V
9、1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R
10、6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2 int temp =MAX;int u=v;for (int j=1;j=n;j+)if (!sj)&(dist j temp)/j 不在 s 中,v 到 j 距离不在为无穷大 u=j;/u保存当前邻接点中距离最小的点的号码temp =dist j;su=true;k+;bk=u;cout -endl;cout 迭代次数:i endl;cout 顶点为:;cout v t;for (int i=1;i=k;i+)cout bi t;cout endl;for (int j=
11、1;j=n;j+)文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码
12、:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10
13、 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9
14、ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档
15、编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V
16、10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L
17、9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2if (!sj)&cu j MAX)int newdist =dist u+cu j;if(newdist dist j)dist j=newdist;/更新 distprev j=u;/记录前驱顶点 cout 单源路径分别为:endl;for
18、(int i=2;i=n;i+)if (dist i!=MAX)cout dist i ;cout endl;cout-endl;/for(int i=1;i=n;i+)/ti=previ;int pLEN;for (int i=2;i=n;i+)cout dist i =dist i ;文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ
19、6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码
20、:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10
21、 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9
22、ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档
23、编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V
24、10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L
25、9 ZQ6P3K9L5W2cout 路径为:v t;/*while(ti!=v)cout ti=1;x-)cout px t;cout i;cout endl;int main()文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:CC8V1I9L3V10 HY3R6C3L5L9 ZQ6P3K9L5W2文档编码:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单源最短 路径 Dijkstra 算法
限制150内