Problem solution · Go

Codeforces 545E — Paths and Trees

Codeforces 545E — Paths and Trees: a Go solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Direct simulation
Source
EndlessCheng Codeforces Go
Length
78 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Codeforces 545E — Paths and Trees, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 78 lines of Go from the credited upstream file 545E.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from EndlessCheng Codeforces Go by Σndless (EndlessCheng) and is used under the MIT licence.

Full codeCodeforces 545E — Paths and Trees · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	"container/heap"	. "fmt"	"io") // https://github.com/EndlessChengfunc cf545E(in io.Reader, _w io.Writer) {	out := bufio.NewWriter(_w)	defer out.Flush()	var n, m, st int	Fscan(in, &n, &m)	type edge struct{ to, wt, i int }	g := make([][]edge, n)	for i := 1; i <= m; i++ {		var v, w, wt int		Fscan(in, &v, &w, &wt)		v--		w--		g[v] = append(g[v], edge{w, wt, i})		g[w] = append(g[w], edge{v, wt, i})	}	Fscan(in, &st)	st-- 	type pair struct{ wt, i int }	from := make([]pair, n)	dis := make([]int, n)	for i := range dis {		dis[i] = 1e18	}	dis[st] = 0	h := hp45{{0, st}}	for len(h) > 0 {		p := heap.Pop(&h).(vd45)		v := p.v		d := p.dis		if d > dis[v] {			continue		}		for _, e := range g[v] {			w, wt := e.to, e.wt			newD := d + wt			if newD < dis[w] {				dis[w] = newD				heap.Push(&h, vd45{newD, w})				from[w] = pair{wt, e.i}			} else if newD == dis[w] {				from[w] = pair{wt, e.i}			}		}	} 	ans := 0	for _, p := range from {		ans += p.wt	}	Fprintln(out, ans)	for i, p := range from {		if i != st {			Fprint(out, p.i, " ")		}	}} //func main() { cf545E(bufio.NewReader(os.Stdin), os.Stdout) } type vd45 struct{ dis, v int }type hp45 []vd45func (h hp45) Len() int           { return len(h) }func (h hp45) Less(i, j int) bool { return h[i].dis < h[j].dis }func (h hp45) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }func (h *hp45) Push(v any)        { *h = append(*h, v.(vd45)) }func (h *hp45) Pop() (v any)      { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return } 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗