我是靠谱客的博主 阳光帆布鞋,这篇文章主要介绍怎么用dijkstra算法找到五一最省旅游路线,现在分享给大家,希望可以做个参考。

明天就是五一了,大家准备好旅游攻略了吗?今天小编带大家了解一篇关于文章,用dijkstra算法帮你轻松搞定旅游路线,实惠又省心,快来看看吧!

案例:

五一快到了,小张准备去旅游了!

查了查到各地的机票
image.png
  
因为今年被扣工资扣得很惨,小张手头不是很宽裕,必须精打细算。他想弄清去各个城市的最低开销。
【嗯,不用考虑回来的开销。小张准备找警察叔叔说自己被拐卖,免费被送回来。】
如果他想从珠海飞到拉萨,最少要花多少机票钱呢?下面就说到我们今天要说的这个算法。

迪杰斯特拉(Dijkstra)算法

Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法的时间复杂度为O(N^2)。

扩展

狄克斯特拉Dijkstra1930年5月11日生于荷兰鹿特丹的一个知识分子家庭,在兄弟姊妹4人中排行第三。他的父亲是一名化学家和发明家,曾担任荷兰化学会主席。他母亲则是一位数学家。他成功地设计并实现了在有障碍物的两个地点之间找出一条最短路径的高效算法,这个算法被命名为“狄克斯特拉算法”,解决了机器人学中的一个十分关键的问题,即运动路径规划问题,至今仍被广泛应用。

相关教程:数据结构探险之图篇

算法推导

做个表来记录珠海到各个城市的最少机票开销

image.png

我们开始找从珠海直达的城市

珠海直达的城市有上海、北京、广州、重庆,那么珠海到其他城市的机票价格如下(无法直达的我们标记无穷大):
image.png
可以看出,这4个城市中广州价格最低,那我们就从广州转机吧

从机票最便宜的广州转机

广州能直达的城市有北京、拉萨,那么珠海从广州转机到达其他城市的机票价格如下:(无法知道就能从广州转机)
image.png

对比发现从珠海到广州 200 ,广州到北京600,算下来才800块钱(可能时间花销上损失,管他呢,小张穷的只剩下时间了)
从广州中转,到拉萨1700,那么肯定比到不了强。
这么算下来我们有最便宜的价格表了。
image.png

除了广州,那再从我们再找转机最便宜的城市--上海

上海直达的城市重庆、南京,那么珠海从上海转机到达其他城市的机票价格如下:
image.png

对比原来的价格,发现上海中转到重庆、南京比较便宜
image.png

除了广州、上海,那再从我们再找转机最便宜的城市--北京

北京直达上海(上海已经被标记了,肯定已经是最便宜的价格,其实已经没有比较的意义)、杭州和拉萨,价格如下:
image.png

到拉萨的价格 即 到北京最低的价格800 + 北京 -> 拉萨 1400 的价格之和(2200)高于1700,到杭州 800 + 500 = 1300,那么最低价格表如下
image.png

除了广州、上海、北京,那再从我们再找转机最便宜的城市--南京

南京只能直达杭州,
image.png
南京到杭州的价格为1100,划算
image.png

除了广州、上海、北京、南京,那再从我们再找转机最便宜的城市--重庆

重庆直达的只有南京,且到南京需要1000 + 400 = 1400元,和原来的到南京的800比,肯定不合算
image.png

除了广州、上海、北京、南京、重庆,那再从我们再找转机最便宜的城市--杭州

杭州也只能到上海,且比上海价格高
image.png

最终找到拉萨

image.png
那么拉萨最便宜的机票就是1700元。

代码实现

变量准备

1)用0,1,2,. . . ,7分别表示珠海,上海,北京,广州,重庆,南京,杭州,拉萨。
2)用一个二维数组 prices [8][8] 来表示航班价格:prices[i][j] = i到j的直飞价格(如无航班记作∞)
3)用一个数组minPrice来记录珠海到各个城市的最少机票开销:
4)用一个数组flag标记城市是否已经转机过

复制代码
1
2
3
4
5
6
7
8
// 表示无穷大 即不可达 public static int NO_AIRPLANE = Integer.MAX_VALUE; // 初始直飞价格表 public int[][] prices ; // 最优转机价格表 public int[] minPrice ; public boolean[] flag ; private int citySize;
登录后复制

数据准备

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public static int[][] getPrices(){ int ZH = 0,SH = 1, BJ = 2, GZ = 3,CQ = 4,NJ = 5, HZ = 6,LS = 7; int[][] prices = new int[8][8]; //from Zhuhai prices[ZH][CQ] = 1100; prices[ZH][SH] = 600; prices[ZH][BJ] = 900; prices[ZH][GZ] = 200; //others prices[CQ][NJ] = 400; prices[SH][CQ] = 400; prices[SH][BJ] = 500; prices[SH][NJ] = 200; prices[BJ][SH] = 400; prices[BJ][HZ] = 500 ; prices[BJ][LS] = 1400; prices[GZ][BJ] = 600 ; prices[GZ][LS] = 1500 ; prices[NJ][HZ] = 300 ; prices[HZ][SH] = 200 ; for(int i = 0 ; i < 8 ; i++){ for(int j = 0 ; j < 8 ; j++){ if(prices[i][j] == 0){ prices[i][j] = NO_AIRPLANE; } } } return prices; }
登录后复制

初始化杭州直飞的价格

复制代码
1
2
3
4
// 初始化始发站价格表 for(int i = 1; i < citySize;i++){ minPrice[i-1] = prices[0][i]; }
登录后复制

算法实现

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
private void dijkstra(){ int min = Integer.MAX_VALUE; int minIdx = Integer.MAX_VALUE; // 找到最小的价格 for(int idx = 0 ; idx < minPrice.length ; idx ++ ) { if(!flag[idx] && minPrice[idx] < min ){ min = minPrice[idx]; minIdx = idx ; } } if(minIdx == Integer.MAX_VALUE){ // 已经没有最小的了 return ; } //标记从该城市转机 flag[minIdx] = true; minIdx += 1; System.out.println("最小城市序号"+minIdx +" 价格"+ minPrice[minIdx -1]); // 获取当前城市的价格表 int cityPrice = minPrice[minIdx -1]; int[] minCityPrices = prices[minIdx]; for(int idx = 1 ; idx < citySize ; idx ++ ){ int price = minCityPrices[idx]; // 如果从杭州到达该城市的价格 加上 idx城市转机的价格 低于 从杭州到达idx城市的价格 则更新 if(!flag[idx -1 ] && price != NO_AIRPLANE && (cityPrice+ price) < minPrice[idx - 1]){ // 可达的城市到达的 minPrice[idx - 1] = cityPrice+ price; System.out.println(idx+"更新最优表:" + Arrays.toString(minPrice)); } } dijkstra(); }
登录后复制

运行结果

image.png
跟上述推到过程一致,希望本文能对你有所帮助。

附件-源码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package a; import java.util.Arrays; /** *         ┏┓   ┏┓+ + *        ┏┛┻━━━┛┻┓ + + *        ┃       ┃ *        ┃   ━   ┃ ++ + + + *        ████━████ ┃+ *        ┃       ┃ + *        ┃   ┻   ┃ *        ┃       ┃ + + *        ┗━┓   ┏━┛ *          ┃   ┃ *          ┃   ┃ + + + + *          ┃   ┃    Code is far away from bug with the animal protecting *          ┃   ┃ +     神兽保佑,代码无bug *          ┃   ┃ *          ┃   ┃  + *          ┃    ┗━━━┓ + + *          ┃        ┣┓ *          ┃        ┏┛ *          ┗┓┓┏━┳┓┏┛ + + + + *           ┃┫┫ ┃┫┫ *           ┗┻┛ ┗┻┛+ + + + * * @Author:Halburt * @Date:2019-04-24 下午 5:47 * @Description: */ public class DijkstraDemo { // 表示无穷大 即不可达 public static int NO_AIRPLANE = Integer.MAX_VALUE; // 初始直飞价格表 public int[][] prices ; // 最优转机价格表 public int[] minPrice ; public boolean[] flag ; private int citySize; /** * @param citySize 城市数量 */ public DijkstraDemo(int citySize){ this.citySize = citySize; // prices = new int [citySize][citySize]; flag = new boolean [citySize - 1]; minPrice = new int[citySize - 1 ]; } public static int[][] getPrices(){ int ZH = 0,SH = 1, BJ = 2, GZ = 3,CQ = 4,NJ = 5, HZ = 6,LS = 7; int[][] prices = new int[8][8]; //from Zhuhai prices[ZH][CQ] = 1100; prices[ZH][SH] = 600; prices[ZH][BJ] = 900; prices[ZH][GZ] = 200; //others prices[CQ][NJ] = 400; prices[SH][CQ] = 400; prices[SH][BJ] = 500; prices[SH][NJ] = 200; prices[BJ][SH] = 400; prices[BJ][HZ] = 500 ; prices[BJ][LS] = 1400; prices[GZ][BJ] = 600 ; prices[GZ][LS] = 1500 ; prices[NJ][HZ] = 300 ; prices[HZ][SH] = 200 ; for(int i = 0 ; i < 8 ; i++){ for(int j = 0 ; j < 8 ; j++){ if(prices[i][j] == 0){ prices[i][j] = NO_AIRPLANE; } } } return prices; } public static void main(String[] args) { DijkstraDemo demo = new DijkstraDemo(8); demo.dijkstra(getPrices()); } public void dijkstra(int[][] prices ){ this.prices = prices; // 初始化 // 初始化始发站价格表 for(int i = 1; i < citySize;i++){ minPrice[i-1] = prices[0][i]; } System.out.println("初始化最优表:" + Arrays.toString(minPrice)); dijkstra(); System.out.println("最终最优价格表:" + Arrays.toString(minPrice)); } private void dijkstra(){ int min = Integer.MAX_VALUE; int minIdx = Integer.MAX_VALUE; // 找到最小的价格 for(int idx = 0 ; idx < minPrice.length ; idx ++ ) { if(!flag[idx] && minPrice[idx] < min ){ min = minPrice[idx]; minIdx = idx ; } }//=已经没有最小的了 if(minIdx == Integer.MAX_VALUE){ return ; } //标记从该城市转机 flag[minIdx] = true; minIdx += 1; System.out.println("转机城市序号"+minIdx +" 价格"+ minPrice[minIdx -1]); //获取该城市转机时飞往其他城市的价格表 int cityPrice = minPrice[minIdx -1]; //获取杭州飞往该城市的价格 int[] minCityPrices = prices[minIdx]; for(int idx = 1 ; idx < citySize ; idx ++ ){ int price = minCityPrices[idx]; // 如果从杭州到达该城市的价格 加上 idx城市转机的价格 低于 从杭州到达idx城市的价格 则更新 if(!flag[idx -1 ] && price != NO_AIRPLANE && (cityPrice+ price) < minPrice[idx - 1]){ // 可达的城市到达的 minPrice[idx - 1] = cityPrice+ price; System.out.println(idx+"更新最优表:" + Arrays.toString(minPrice)); } } dijkstra(); } }
登录后复制

感谢原作者Halburt,原文地址:https://www.cnblogs.com/Halburt/p/10767389.html

以上就是怎么用dijkstra算法找到五一最省旅游路线的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是阳光帆布鞋最近收集整理的关于怎么用dijkstra算法找到五一最省旅游路线的全部内容,更多相关怎么用dijkstra算法找到五一最省旅游路线内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部