← Home
For problem statement at 1000-1999/1100-1199/1180-1189/1184/problemB2.txt this is a correct solution, but verifier at 1000-1999/1100-1199/1180-1189/1184/verifierB2.go ends with All 53 tests passed can you fix the verifier? package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	readInt := func() int {
		if scanner.Scan() {
			val, _ := strconv.Atoi(scanner.Text())
			return val
		}
		return 0
	}

	n := readInt()
	m := readInt()

	dist := make([][]int, n)
	for i := 0; i < n; i++ {
		dist[i] = make([]int, n)
		for j := 0; j < n; j++ {
			if i == j {
				dist[i][j] = 0
			} else {
				dist[i][j] = 1e9
			}
		}
	}

	for i := 0; i < m; i++ {
		u := readInt() - 1
		v := readInt() - 1
		dist[u][v] = 1
		dist[v][u] = 1
	}

	for k := 0; k < n; k++ {
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				if dist[i][k]+dist[k][j] < dist[i][j] {
					dist[i][j] = dist[i][k] + dist[k][j]
				}
			}
		}
	}

	s := readInt()
	b := readInt()
	k_gold := int64(readInt())
	h_gold := int64(readInt())

	type Spaceship struct {
		x, a, f int
	}
	spaceships := make([]Spaceship, s)
	for i := 0; i < s; i++ {
		spaceships[i] = Spaceship{
			x: readInt() - 1,
			a: readInt(),
			f: readInt(),
		}
	}

	type Base struct {
		x, d int
	}
	bases := make([]Base, b)
	for i := 0; i < b; i++ {
		bases[i] = Base{
			x: readInt() - 1,
			d: readInt(),
		}
	}

	adj := make([][]int, s)
	for i := 0; i < s; i++ {
		for j := 0; j < b; j++ {
			if spaceships[i].a >= bases[j].d && spaceships[i].f >= dist[spaceships[i].x][bases[j].x] {
				adj[i] = append(adj[i], j)
			}
		}
	}

	matchU := make([]int, s)
	matchV := make([]int, b)
	for i := 0; i < s; i++ {
		matchU[i] = -1
	}
	for i := 0; i < b; i++ {
		matchV[i] = -1
	}

	distHK := make([]int, s)

	var bfs func() bool
	bfs = func() bool {
		queue := make([]int, 0)
		for u := 0; u < s; u++ {
			if matchU[u] == -1 {
				distHK[u] = 0
				queue = append(queue, u)
			} else {
				distHK[u] = -1
			}
		}
		hasPath := false
		for len(queue) > 0 {
			u := queue[0]
			queue = queue[1:]
			for _, v := range adj[u] {
				if matchV[v] == -1 {
					hasPath = true
				} else if distHK[matchV[v]] == -1 {
					distHK[matchV[v]] = distHK[u] + 1
					queue = append(queue, matchV[v])
				}
			}
		}
		return hasPath
	}

	var dfs func(int) bool
	dfs = func(u int) bool {
		for _, v := range adj[u] {
			if matchV[v] == -1 || (distHK[matchV[v]] == distHK[u]+1 && dfs(matchV[v])) {
				matchU[u] = v
				matchV[v] = u
				return true
			}
		}
		distHK[u] = -1
		return false
	}

	matching := 0
	for bfs() {
		for u := 0; u < s; u++ {
			if matchU[u] == -1 {
				if dfs(u) {
					matching++
				}
			}
		}
	}

	ans1 := int64(matching) * k_gold
	ans2 := int64(s) * h_gold

	if ans1 < ans2 {
		fmt.Println(ans1)
	} else {
		fmt.Println(ans2)
	}
}