package main import ( "bufio" "container/heap" "fmt" "math" "os" "strconv" ) func getScanner(fp *os.File) *bufio.Scanner { scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) return scanner } func getNextString(scanner *bufio.Scanner) string { scanner.Scan() return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextUint64(scanner *bufio.Scanner) uint64 { i, _ := strconv.ParseUint(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout cnt := 0 if os.Getenv("MASPY") == "ますピ" { fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT")) cnt = 2 } if os.Getenv("MASPYPY") == "ますピッピ" { wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10")) } scanner := getScanner(fp) writer := bufio.NewWriter(wfp) solve(scanner, writer) for i := 0; i < cnt; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } writer.Flush() } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) c := getNextInt(scanner) v := getNextInt(scanner) ss := make([]int, v) tt := make([]int, v) yy := make([]int, v) mm := make([]int, v) for i := 0; i < v; i++ { ss[i] = getNextInt(scanner) - 1 } for i := 0; i < v; i++ { tt[i] = getNextInt(scanner) - 1 } for i := 0; i < v; i++ { yy[i] = getNextInt(scanner) } for i := 0; i < v; i++ { mm[i] = getNextInt(scanner) } g := newGraph(v) for i := 0; i < v; i++ { g.v[i] = &vertex{ ym: make([]int, c+1), } for j := 0; j < c+1; j++ { g.v[i].ym[j] = -1 } g.appendEdge(ss[i], tt[i], i, yy[i], mm[i]) g.appendEdge(tt[i], ss[i], i, yy[i], mm[i]) } dd := &daikes{} heap.Init(dd) heap.Push(dd, newDaike(0, c, 0)) for dd.Len() > 0 { p := heap.Pop(dd).(daike) if g.v[p.i].ym[p.y] != -1 { continue } g.v[p.i].ym[p.y] = p.m for _, e := range g.e[p.i] { if p.y-e.y < 0 { continue } if g.v[e.to].ym[p.y-e.y] == -1 { heap.Push(dd, newDaike(e.to, p.y-e.y, p.m+e.m)) } } } ans := cint(math.MaxInt32) for i := 0; i < c; i++ { if g.v[n-1].ym[i] == -1 { continue } ans.min(cint(g.v[n-1].ym[i])) } if ans == cint(math.MaxInt32) { ans = -1 } fmt.Fprintln(writer, ans) } type cint int func (x *cint) min(y cint) *cint { if *x > y { *x = y } return x } func (x *cint) max(y cint) *cint { if *x < y { *x = y } return x } func newDaike(i, y, m int) daike { return daike{ i: i, y: y, m: m, } } type daike struct { i, y, m int } type daikes []daike func (h daikes) Len() int { return len(h) } func (h daikes) Less(i, j int) bool { return h[i].m < h[j].m } func (h daikes) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *daikes) Push(x interface{}) { *h = append(*h, x.(daike)) } func (h *daikes) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } type graph struct { v []*vertex e [][]*edge } func newGraph(n int) *graph { return &graph{ v: make([]*vertex, n), e: make([][]*edge, n), } } func (g *graph) appendEdge(from, to, id, y, m int) { g.e[from] = append(g.e[from], &edge{ to: to, id: id, y: y, m: m, }) } type vertex struct { ym []int } type edge struct { to, id, y, m int }