动态规划之最短路径(Floyd算法)
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func floyd(m [][]int) { 8 length := len(m[0]) 9 var min, i, j int 10 for k := 0; k < length; k++ { 11 for i = 0; i < length; i++ { 12 for j = 0; j < length; j++ { 13 if m[i][k] > 0 && m[k][j] > 0 { 14 min = m[i][k] + m[k][j] 15 if min < m[i][j] || m[i][j] < 0{ 16 m[i][j] = min 17 } 18 } 19 } 20 } 21 } 22 for i = 0; i < length; i++ { 23 for j = 0; j < length; j++ { 24 fmt.Print(m[i][j], "\t") 25 } 26 fmt.Println("") 27 } 28 } 29 30 func main() { 31 m := [][]int{{0, 2, 9}, {8, 0, -1}, {1, -1, 0}} 32 floyd(m) 33 }