Problem solution · Go

Codeforces 1366F — Jog Around The Graph

Codeforces 1366F — Jog Around The Graph: 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
94 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 1366F — Jog Around The Graph, 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

  • 94 lines of Go from the credited upstream file 1366F.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 1366F — Jog Around The Graph · GoGo
Use this to learn the idea, then write your own version.
package main import (	"cmp"	. "fmt"	"io"	"slices") // https://github.com/EndlessChengtype vec66 struct{ x, y int } func (a vec66) sub(b vec66) vec66 { return vec66{a.x - b.x, a.y - b.y} }func (a vec66) dot(b vec66) int   { return a.x*b.x + a.y*b.y }func (a vec66) det(b vec66) int   { return a.x*b.y - a.y*b.x } func cf1366F(in io.Reader, out io.Writer) {	const mod = 1_000_000_007	var n, m, k, ans int	Fscan(in, &n, &m, &k)	type nb struct{ to, wt int }	g := make([][]nb, n)	for range m {		var v, w, wt int		Fscan(in, &v, &w, &wt)		v--		w--		g[v] = append(g[v], nb{w, wt})		g[w] = append(g[w], nb{v, wt})	} 	f := make([]int, n)	for i := 1; i < n; i++ {		f[i] = -1e18	}	for range m {		nf := make([]int, n)		for i := range nf {			nf[i] = -1e18		}		for v, fv := range f {			if fv < 0 {				continue			}			for _, e := range g[v] {				nf[e.to] = max(nf[e.to], fv+e.wt)			}		}		f = nf		ans += slices.Max(f)	} 	a := make([]vec66, 0, n)	for i, fv := range f {		if fv < 0 {			continue		}		mx := 0		for _, e := range g[i] {			mx = max(mx, e.wt)		}		a = append(a, vec66{mx, fv})	}	slices.SortFunc(a, func(a, b vec66) int { return cmp.Or(a.x-b.x, a.y-b.y) })	q := a[:0]	for _, v := range a {		for len(q) > 1 && q[len(q)-1].sub(q[len(q)-2]).det(v.sub(q[len(q)-1])) >= 0 {			q = q[:len(q)-1]		}		q = append(q, v)	}	if len(q) > 1 && q[0].x == q[1].x {		q = q[1:]	} 	k -= m	i := 1	for len(q) > 1 {		nxt := (q[0].y-q[1].y)/(q[1].x-q[0].x) + 1		if nxt > k {			break		}		if nxt > i {			ans = (ans + (i+nxt-1)*(nxt-i)/2%mod*q[0].x + (nxt-i)*q[0].y) % mod			i = nxt		}		q = q[1:]	}	ans = (ans + (i+k)*(k-i+1)/2%mod*q[0].x + (k-i+1)*q[0].y) % mod	Fprint(out, ans)} //func main() { cf1366F(bufio.NewReader(os.Stdin), os.Stdout) } 

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 ↗