30 lines
748 B
Go
30 lines
748 B
Go
package q3650
|
|
|
|
func minCost(n int, edges [][]int) int {
|
|
connections := make([][][2]int, n)
|
|
for i := range edges {
|
|
f, t, w := edges[i][0], edges[i][1], edges[i][2]
|
|
connections[f] = append(connections[f], [2]int{t, w})
|
|
connections[t] = append(connections[t], [2]int{f, w * 2})
|
|
}
|
|
minCost := make([]int, n)
|
|
minCost[0] = 1
|
|
|
|
queue := make([]int, 0, 1024)
|
|
queue = append(queue, 0)
|
|
for ; len(queue) > 0; queue = queue[1:] {
|
|
node := queue[0]
|
|
|
|
for i := range connections[node] {
|
|
next, cost := connections[node][i][0], connections[node][i][1]
|
|
if minCost[next] == 0 || minCost[node]+cost < minCost[next] {
|
|
minCost[next] = minCost[node] + cost
|
|
queue = append(queue, next)
|
|
}
|
|
}
|
|
}
|
|
|
|
return minCost[n-1] - 1
|
|
}
|
|
|
|
var _ = minCost
|