結果

問題 No.160 最短経路のうち辞書順最小
ユーザー noriocnorioc
提出日時 2015-08-16 13:29:19
言語 Scala(Beta)
(3.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,586 bytes
コンパイル時間 5,925 ms
コンパイル使用メモリ 236,348 KB
最終ジャッジ日時 2024-04-27 02:09:35
合計ジャッジ時間 6,333 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
-- [E040] Syntax Error: Main.scala:16:71 ---------------------------------------
16 |  def calc(n: Int, s: Int, g: Int, nodes: ListBuffer[(Int, Int, Int)]) {
   |                                                                       ^
   |                                             '=' expected, but '{' found
-- [E040] Syntax Error: Main.scala:51:32 ---------------------------------------
51 |  def main(args: Array[String]) {
   |                                ^
   |                                '=' expected, but '{' found
2 errors found

ソースコード

diff #

import scala.collection.mutable.PriorityQueue
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.HashSet

object Main {
  val sc = new java.util.Scanner(System.in)

  def readInts(): Array[Int] =
    sc.nextLine.split(' ').map(_.toInt)

  def Ord = new Ordering[(Int, Int, Int)] {
    def compare(a: (Int, Int, Int), b: (Int, Int, Int)): Int =
      -1 * a._3.compare(b._3)
  }

  def calc(n: Int, s: Int, g: Int, nodes: ListBuffer[(Int, Int, Int)]) {
    val q = new PriorityQueue[(Int, Int, Int)]()(Ord) // (from, to, cost)
    q.enqueue((0, g, 0))

    val pathcost = Array.fill(n)(Int.MaxValue)
    val prev = Array.fill(n)(new ListBuffer[Int])
    while (!q.isEmpty) {
      val (from, to, cost) = q.dequeue

      if (cost == pathcost(to)) {
        prev(to).append(from)
      }
      else if (cost < pathcost(to)) {
        pathcost(to) = cost
        prev(to).append(from)
        // println("visit:", to, "cost:", cost)

        for ((a, b, c) <- nodes) {
          if (a == to)
            q.enqueue((to, b, cost + c))
          else if (b == to)
            q.enqueue((to, a, cost + c))
        }
      }
    }

    // 経路復元
    val path = new ListBuffer[Int]
    path.append(s)
    while (path.last != g) {
      path.append(prev(path.last).min)
    }
    println(path.mkString(" "))
  }

  def main(args: Array[String]) {
    val Array(n, m, s, g) = readInts()

    val nodes = new ListBuffer[(Int, Int, Int)]
    for (i <- 0 to m-1) {
      val Array(a, b, c) = readInts()
      nodes.append((a, b, c))
    }
    calc(n, s, g, nodes)
  }
}
0