shortTablePath
Posted li仲玄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shortTablePath相关的知识,希望对你有一定的参考价值。
package main import ( "fmt" ) const MAXVEX int = 9 const MAXWEIGHT int = 1000 var shortTablePath = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} func main() { graph := NewGraph() var TablePathMin int //存放shortTablePath中,未遍历的最小结点的值 var Vx int //存放shortTablePath中,未遍历的最小结点的下标 var isgetPath [MAXVEX]bool //记录结点是否已经找到v0到vx的最小路径 // 获取v0这一行的权值数组 for v := 0; v < len(graph); v++ { shortTablePath[v] = graph[0][v] } shortTablePath[0] = 0 isgetPath[0] = true //遍历v1 ~ v8 for v := 1; v < len(graph); v++ { TablePathMin = MAXWEIGHT //找出shortTablePath中,未遍历的最小结点的值 for w := 0; w < len(graph); w++ { if !isgetPath[w] && shortTablePath[w] < TablePathMin { Vx = w TablePathMin = shortTablePath[w] } } isgetPath[Vx] = true for j := 0; j < len(graph); j++ { if !isgetPath[j] && TablePathMin+graph[Vx][j] < shortTablePath[j] { shortTablePath[j] = TablePathMin + graph[Vx][j] } } fmt.Println("遍历完V", v, "后:", shortTablePath) } //输出 for i := 0; i < len(shortTablePath); i++ { fmt.Println("V0到V", i, "最小路径:", shortTablePath[i]) } } func NewGraph() [MAXVEX][MAXVEX]int { var graph [MAXVEX][MAXVEX]int var v0 = [MAXVEX]int{0, 1, 5, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} var v1 = [MAXVEX]int{1, 0, 3, 7, 5, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} var v2 = [MAXVEX]int{5, 3, 0, MAXWEIGHT, 1, 7, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT} var v3 = [MAXVEX]int{MAXWEIGHT, 7, MAXWEIGHT, 0, 2, MAXWEIGHT, 3, MAXWEIGHT, MAXWEIGHT} var v4 = [MAXVEX]int{MAXWEIGHT, 5, 1, 2, 0, 3, 6, 9, MAXWEIGHT} var v5 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, 7, MAXWEIGHT, 3, 0, MAXWEIGHT, 5, MAXWEIGHT} var v6 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 3, 6, MAXWEIGHT, 0, 2, 7} var v7 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 9, 5, 2, 0, 4} var v8 = [MAXVEX]int{MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, MAXWEIGHT, 7, 4, 0} graph[0] = v0 graph[1] = v1 graph[2] = v2 graph[3] = v3 graph[4] = v4 graph[5] = v5 graph[6] = v6 graph[7] = v7 graph[8] = v8 return graph }
遍历完V 1 后: [0 1 4 8 6 1000 1000 1000 1000]
遍历完V 2 后: [0 1 4 8 5 11 1000 1000 1000]
遍历完V 3 后: [0 1 4 7 5 8 11 14 1000]
遍历完V 4 后: [0 1 4 7 5 8 10 14 1000]
遍历完V 5 后: [0 1 4 7 5 8 10 13 1000]
遍历完V 6 后: [0 1 4 7 5 8 10 12 17]
遍历完V 7 后: [0 1 4 7 5 8 10 12 16]
遍历完V 8 后: [0 1 4 7 5 8 10 12 16]
V0到V 0 最小路径: 0
V0到V 1 最小路径: 1
V0到V 2 最小路径: 4
V0到V 3 最小路径: 7
V0到V 4 最小路径: 5
V0到V 5 最小路径: 8
V0到V 6 最小路径: 10
V0到V 7 最小路径: 12
V0到V 8 最小路径: 16
以上是关于shortTablePath的主要内容,如果未能解决你的问题,请参考以下文章