Problem solution · Go

Codeforces 715B — Complete The Graph

Codeforces 715B — Complete The Graph: a Go solution using stack-based processing. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Stack-based processing
Source
EndlessCheng Codeforces Go
Length
100 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

Stack-based processing

For Codeforces 715B — Complete The Graph, the implementation keeps unresolved items in last-in, first-out order, often to match boundaries, parse structure, or maintain monotonic candidates.

  1. Define what every stack entry represents.
  2. Pop entries once the current item resolves or invalidates them.
  3. Push the current item with only the information later steps need.

Code notes

  • 100 lines of Go from the credited upstream file 715B.go.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

If each item is pushed and popped at most once, the stack work is linear.

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 715B — Complete The Graph · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	"container/heap"	. "fmt"	"io") // github.com/EndlessCheng/codeforces-gotype pair15 struct {	v int	d int64}type hp15 []pair15 func (h hp15) Len() int              { return len(h) }func (h hp15) Less(i, j int) bool    { return h[i].d < h[j].d }func (h hp15) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }func (h *hp15) Push(v interface{})   { *h = append(*h, v.(pair15)) }func (h *hp15) Pop() (v interface{}) { a := *h; *h, v = a[:len(a)-1], a[len(a)-1]; return }func (h *hp15) push(v pair15)        { heap.Push(h, v) }func (h *hp15) pop() pair15          { return heap.Pop(h).(pair15) } func CF715B(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush() 	var n, m, s, t, v, w int	var l, wt, diff int64	Fscan(in, &n, &m, &l, &s, &t)	type nb struct {		to, rid int		wt      int64		del     bool	}	g := make([][]nb, n)	for ; m > 0; m-- {		Fscan(in, &v, &w, &wt)		del := wt == 0		if del {			wt = 1		}		g[v] = append(g[v], nb{w, len(g[w]), wt, del})		g[w] = append(g[w], nb{v, len(g[v]) - 1, wt, del})	} 	dis := make([][2]int64, n)	for i := range dis {		dis[i] = [2]int64{1e18, 1e18}	}	dis[s] = [2]int64{}	dij := func(k int) {		h := hp15{{s, 0}}		for len(h) > 0 {			p := h.pop()			v := p.v			if dis[v][k] < p.d {				continue			}			for i, e := range g[v] {				w, wt := e.to, e.wt				if e.del && k > 0 {					if newD := dis[w][0] + diff - dis[v][1]; newD > wt {						wt = newD						g[v][i].wt = newD						g[w][e.rid].wt = newD					}				}				if newD := dis[v][k] + wt; newD < dis[w][k] {					dis[w][k] = newD					h.push(pair15{w, newD})				}			}		}	}	dij(0)	diff = l - dis[t][0]	if diff < 0 {		Fprint(out, "NO")		return	}	dij(1)	if dis[t][1] < l {		Fprint(out, "NO")		return	}	Fprintln(out, "YES")	for v, es := range g {		for _, e := range es {			if e.to > v {				Fprintln(out, v, e.to, e.wt)			}		}	}} //func main() { CF715B(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 ↗