@@ -114,7 +114,7 @@ dijkstra 算法 同样是贪心的思路,不断寻找距离 源点最近的没
114
114
115
115
### 模拟过程
116
116
117
- -----------
117
+ -----------
118
118
119
119
0、初始化
120
120
@@ -130,7 +130,7 @@ minDist数组数值初始化为int最大值。
130
130
131
131
此时所有节点都没有被访问过,所以 visited数组都为0
132
132
133
- ---------------
133
+ ---------------
134
134
135
135
以下为dijkstra 三部曲
136
136
@@ -555,13 +555,13 @@ int main() {
555
555
556
556
那我们来看dijkstra 求解的路径是什么样的,继续dijkstra 三部曲来模拟 :(dijkstra模拟过程上面已经详细讲过,以下只模拟重要过程,例如如何初始化就省略讲解了)
557
557
558
- -----------
558
+ -----------
559
559
560
560
初始化:
561
561
562
562
![ ] ( https://file1.kamacoder.com/i/algo/20240227104801.png )
563
563
564
- ---------------
564
+ ---------------
565
565
566
566
1、选源点到哪个节点近且该节点未被访问过
567
567
@@ -632,7 +632,7 @@ int main() {
632
632
633
633
节点5的加入,而节点5 没有链接其他节点, 所以不用更新minDist数组,仅标记节点5被访问过了
634
634
635
- ------------
635
+ ------------
636
636
637
637
1、选源点到哪个节点近且该节点未被访问过
638
638
@@ -646,7 +646,7 @@ int main() {
646
646
647
647
![ ] ( https://file1.kamacoder.com/i/algo/20240227110711.png )
648
648
649
- --------------
649
+ --------------
650
650
651
651
至此dijkstra的模拟过程就结束了,根据最后的minDist数组,我们求 节点1 到 节点5 的最短路径的权值总和为 3,路径: 节点1 -> 节点3 -> 节点4 -> 节点5
652
652
@@ -865,6 +865,114 @@ if __name__ == "__main__":
865
865
866
866
### Go
867
867
868
+ ``` go
869
+ package main
870
+
871
+ import (
872
+ " fmt"
873
+ " os"
874
+ " bufio"
875
+ " strconv"
876
+ " strings"
877
+ " math"
878
+ )
879
+
880
+ func main () {
881
+ // 创建Reader从标准输入中读取数据
882
+ reader := bufio.NewReader (os.Stdin )
883
+
884
+ // 以字符串的形式读取一行
885
+ line , _ := reader.ReadString (' \n ' )
886
+ // 去掉字符串前后可能存在的空格
887
+ line = strings.TrimSpace (line)
888
+ // 以空格作为分隔符分割字符串,得到数字的字符串形式
889
+ params := strings.Split (line, " " )
890
+ // 字符串转化为数字,得到n和m,其中n为汽车站数,m为公路数
891
+ n , _ := strconv.Atoi (params[0 ])
892
+ m , _ := strconv.Atoi (params[1 ])
893
+
894
+ // 存储从源点到每个节点的最短距离
895
+ minDist := initSliceInt (math.MaxInt32 , n + 1 )
896
+ minDist[1 ] = 0
897
+ // 记录顶点是否被访问过
898
+ visited := initSliceBool (false , n + 1 )
899
+
900
+ // 存储每个车站之间的距离
901
+ grid := make ([][]int , n + 1 )
902
+ for i := 1 ; i <= n; i++ {
903
+ grid[i] = initSliceInt (math.MaxInt32 , n + 1 )
904
+ grid[i][i] = 0
905
+ }
906
+ for i := 1 ; i <= m; i++ {
907
+ line, _ = reader.ReadString (' \n ' )
908
+ line = strings.TrimSpace (line)
909
+ params = strings.Split (line, " " )
910
+ a , _ := strconv.Atoi (params[0 ])
911
+ b , _ := strconv.Atoi (params[1 ])
912
+ c , _ := strconv.Atoi (params[2 ])
913
+
914
+ grid[a][b] = c
915
+ }
916
+
917
+ // Dijkstra算法
918
+ for i := 1 ; i <= n; i++ {
919
+ cur := -1
920
+ // 1、选距离源点最近且未访问过的节点
921
+ for j := 1 ; j <= n; j++ {
922
+ if visited[j] == false && (cur == -1 || minDist[cur] > minDist[j]) {
923
+ cur = j;
924
+ }
925
+ }
926
+
927
+ // 2、标记该节点已被访问
928
+ visited[cur] = true
929
+
930
+ /*
931
+ 3、更新非访问节点到源点的距离(即更新minDist数组)。实际更新时无需判断
932
+ 节点是否被访问过,因为1.的限制,即使更新被访问过的点也没有任何影响。
933
+ */
934
+ for j := 1 ; j <= n; j++ {
935
+ minDist[j] = min (minDist[j], minDist[cur] + grid[cur][j])
936
+ }
937
+
938
+ }
939
+
940
+ if minDist[n] == math.MaxInt32 {
941
+ // 不能到达终点
942
+ fmt.Print (-1 )
943
+ } else {
944
+ // 到达终点最短路径
945
+ fmt.Print (minDist[n])
946
+ }
947
+ }
948
+
949
+ // 创建int类型的切片
950
+ func initSliceInt (value int , count int ) []int {
951
+ result := make ([]int , count)
952
+ for i := range result {
953
+ result[i] = value
954
+ }
955
+ return result
956
+ }
957
+
958
+ // 创建bool类型的切片
959
+ func initSliceBool (value bool , count int ) []bool {
960
+ result := make ([]bool , count)
961
+ for i := range result {
962
+ result[i] = value
963
+ }
964
+ return result
965
+ }
966
+
967
+ // 比较两个int类型的大小,返回较小的一个
968
+ func min (a , b int ) int {
969
+ if a > b {
970
+ return b
971
+ }
972
+ return a
973
+ }
974
+ ```
975
+
868
976
### Rust
869
977
870
978
### JavaScript
0 commit comments