← Home
For problem statement at 0-999/0-99/20-29/24/problemA.txt this is a correct solution, but verifier at 0-999/0-99/20-29/24/verifierA.go ends with All tests passed can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"os"
)

type Edge struct {
	to   int
	dir  bool
	cost int
}

func computeCost(start, first int, adj [][]Edge, n int) int {
	cost := 0
	prev := start
	curr := first
	for i := 0; i < n; i++ {
		edges := adj[prev]
		var e Edge
		if edges[0].to == curr {
			e = edges[0]
		} else {
			e = edges[1]
		}
		if !e.dir {
			cost += e.cost
		}
		nextEdges := adj[curr]
		var next int
		if nextEdges[0].to != prev {
			next = nextEdges[0].to
		} else {
			next = nextEdges[1].to
		}
		prev, curr = curr, next
	}
	return cost
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n int
	if _, err := fmt.Fscan(in, &n); err != nil {
		return
	}

	adj := make([][]Edge, n+1)
	total := 0
	for i := 0; i < n; i++ {
		var a, b, c int
		fmt.Fscan(in, &a, &b, &c)
		adj[a] = append(adj[a], Edge{to: b, dir: true, cost: c})
		adj[b] = append(adj[b], Edge{to: a, dir: false, cost: c})
		total += c
	}

	start := 1
	first := adj[start][0].to
	cost1 := computeCost(start, first, adj, n)
	ans := cost1
	if total-cost1 < ans {
		ans = total - cost1
	}
	fmt.Fprintln(out, ans)
}