add new solutions
This commit is contained in:
parent
67cad91898
commit
51975f3386
24 changed files with 933 additions and 14 deletions
30
solutions/36/q3650/solution.go
Normal file
30
solutions/36/q3650/solution.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue