結果

問題 No.1 道のショートカット
ユーザー くわい
提出日時 2015-11-05 14:41:57
言語 Scala(Beta)
(3.6.2)
結果
AC  
実行時間 1,151 ms / 5,000 ms
コード長 1,531 bytes
コンパイル時間 13,103 ms
コンパイル使用メモリ 275,760 KB
実行使用メモリ 73,736 KB
最終ジャッジ日時 2024-06-11 13:33:15
合計ジャッジ時間 54,765 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
  }
}
0