add new solutions

This commit is contained in:
kanna5 2026-01-27 18:38:43 +09:00
parent 67cad91898
commit eb6ffe8114
No known key found for this signature in database
24 changed files with 933 additions and 14 deletions

View 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