import java.util.Scanner import scala.collection.mutable object Problem001 { case class Route(start: Int, to: Int, cost: Int, dist: Int) case class State(currentCity: Int, totalCost: Int, totalDist: Int) def proc(cityCount: Int, costLimit: Int, routeCount: Int, start: IndexedSeq[Int], to: IndexedSeq[Int], costs: IndexedSeq[Int], dists: IndexedSeq[Int]): Int = { // 町から出る道のリスト val routes: Map[Int, Seq[Route]] = ((0 until routeCount) map { i => Route(start(i), to(i), costs(i), dists(i)) }).groupBy(x => x.start) def f: (State) => Int = x => x.totalDist val queue = new mutable.PriorityQueue[State]()(Ordering.by(f).reverse) // 開始地点設定 queue += State(1, 0, 0) while (queue.nonEmpty) { val s: State = queue.dequeue() if (s.currentCity == cityCount) { return s.totalDist } for (route <- routes.getOrElse(s.currentCity, Seq())) { if (s.totalCost + route.cost <= costLimit) { queue += State(route.to, s.totalCost + route.cost, s.totalDist + route.dist) } } } -1 } def main(args: Array[String]) = { val sc = new Scanner(System.in) val N = sc.nextInt() val C = sc.nextInt() val V = sc.nextInt() val S = IndexedSeq.fill(V)(sc.nextInt()) val T = IndexedSeq.fill(V)(sc.nextInt()) val Y = IndexedSeq.fill(V)(sc.nextInt()) val M = IndexedSeq.fill(V)(sc.nextInt()) val result = proc(N, C, V, S, T, Y, M) println(result) } }