Problem solution · Go

Codeforces 808F — Card Game

Codeforces 808F — Card Game: a Go solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to EndlessCheng Codeforces Go.

Technique
Breadth-first search
Source
EndlessCheng Codeforces Go
Length
130 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

Breadth-first search

For Codeforces 808F — Card Game, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 130 lines of Go from the credited upstream file 808F.go.
  • The implementation visibly relies on sequence storage.
  • 1 loop block detected, together with recursive traversal.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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 808F — Card Game · GoGo
Use this to learn the idea, then write your own version.
package main import (	"bufio"	. "fmt"	"io"	"sort") // github.com/EndlessCheng/codeforces-gofunc CF808F(_r io.Reader, _w io.Writer) {	in := bufio.NewReader(_r)	out := bufio.NewWriter(_w)	defer out.Flush()	const inf int = 1e9	type card struct{ p, c, l int }	type neighbor struct{ to, rid, cap int }	min := func(a, b int) int {		if a < b {			return a		}		return b	}	const mx int = 2e5	isP := [mx + 1]bool{}	for i := range isP {		isP[i] = true	}	for i := 2; i <= mx; i++ {		if isP[i] {			for j := 2 * i; j <= mx; j += i {				isP[j] = false			}		}	} 	var n, minP int	Fscan(in, &n, &minP)	a := make([]card, n)	for i := range a {		Fscan(in, &a[i].p, &a[i].c, &a[i].l)	}	ans := sort.Search(n+1, func(upL int) bool {		g := make([][]neighbor, n+3)		addEdge := func(from, to, cap int) {			g[from] = append(g[from], neighbor{to, len(g[to]), cap})			g[to] = append(g[to], neighbor{from, len(g[from]) - 1, 0})		}		st, end, sumP, mxI := n+1, n+2, 0, -1		for i, c := range a {			if c.l <= upL && c.c == 1 && (mxI == -1 || c.p > a[mxI].p) {				mxI = i			}		}		// 源连奇数,偶数连汇		// 奇数连偶数,特别地,所有 1 中仅选择一个 p 最大的连偶数		for i, c := range a {			if c.l > upL || c.c == 1 && i != mxI {				continue			}			sumP += c.p			if c.c&1 > 0 {				addEdge(st, i, c.p)				for j, d := range a {					if d.l <= upL && d.c&1 == 0 && isP[c.c+d.c] {						addEdge(i, j, inf)					}				}			} else {				addEdge(i, end, c.p)			}		} 		d := make([]int, n+3)		bfs := func() bool {			for i := range d {				d[i] = -1			}			d[st] = 0			q := []int{st}			for len(q) > 0 {				v := q[0]				q = q[1:]				for _, e := range g[v] {					if w := e.to; e.cap > 0 && d[w] < 0 {						d[w] = d[v] + 1						q = append(q, w)					}				}			}			return d[end] >= 0		}		var iter []int		var dfs func(int, int) int		dfs = func(v, minF int) int {			if v == end {				return minF			}			for ; iter[v] < len(g[v]); iter[v]++ {				e := &g[v][iter[v]]				if w := e.to; e.cap > 0 && d[w] > d[v] {					if f := dfs(w, min(minF, e.cap)); f > 0 {						e.cap -= f						g[w][e.rid].cap += f						return f					}				}			}			return 0		}		for bfs() {			iter = make([]int, n+3)			for {				if f := dfs(st, inf); f > 0 {					sumP -= f // 减去最小割				} else {					break				}			}		}		return sumP >= minP	})	if ans > n {		ans = -1	}	Fprint(out, ans)} //func main() { CF808F(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 ↗