From 72ba4c2713381060d8b5bbbb386c8390531386fa Mon Sep 17 00:00:00 2001 From: AbrahamRH Date: Mon, 13 Feb 2023 21:15:57 -0600 Subject: [PATCH] Adding the solution for the chain reaction problem, resolved in Golang --- solutions/chain-reactions/Go/chainReaction.go | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 solutions/chain-reactions/Go/chainReaction.go diff --git a/solutions/chain-reactions/Go/chainReaction.go b/solutions/chain-reactions/Go/chainReaction.go new file mode 100644 index 00000000..884a1359 --- /dev/null +++ b/solutions/chain-reactions/Go/chainReaction.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + "math" +) + +// Just for the reading of the test cases +func main() { + var T int + fmt.Scanf("%d", &T) + + for caseNumber := 1; caseNumber <= T; caseNumber++ { + var N int + fmt.Scanf("%d", &N) + + F := make([]int64, N+1) + for i := 1; i < N+1; i++ { + fmt.Scanf("%d", &F[i]) + } + + P := make([]int64, N+1) + for i := 1; i < N+1; i++ { + fmt.Scanf("%d", &P[i]) + } + + // Machines that point to each other + machines := make(map[int64][]int64) + for i := 1; i < N+1; i++ { + machines[P[i]] = append(machines[P[i]], int64(i)) + } + + Fun, _ := chainReaction(0, machines, F) + fmt.Printf("Case #%d: %d\n", caseNumber, Fun) + } +} + +// The solution of the actual problem +func chainReaction(root int64, machines map[int64][]int64, F []int64) (maxF int64, minV int64) { + if len(machines[root]) == 0 { + return F[root], F[root] + } else { + maxF = 0 + minV = math.MaxInt64 + for _, machine := range machines[root] { + total, low := chainReaction(machine, machines, F) + maxF += total + minV = int64(math.Min(float64(minV), float64(low))) + } + } + + if minV < int64(F[root]) { + maxF += F[root] - minV + minV = F[root] + } + + return +}