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
| func networkDelayTime(times [][]int, n int, k int) int { dist := make([]int, n+1) const inf = math.MaxInt / 2
for i, _ := range dist { if i == k { dist[i] = 0 } else { dist[i] = inf } }
fmt.Println(dist) for j := 1; j < n; j++ { flag := false for i := 0; i < len(times); i++ { x := times[i][0] y := times[i][1] z := times[i][2]
if dist[y] > dist[x] + z { dist[y] = dist[x] + z flag = true } } if !flag { break } }
ans := 0
for i := 1; i < len(dist); i++ { if dist[i] == inf { return -1 }
if ans < dist[i] { ans = dist[i] } }
return ans }
|