結果

問題 No.1812 Uribo Road
ユーザー yudedakoyudedako
提出日時 2022-01-25 19:37:50
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 2,642 bytes
コンパイル時間 18,263 ms
コンパイル使用メモリ 269,292 KB
実行使用メモリ 77,008 KB
最終ジャッジ日時 2024-05-09 20:41:03
合計ジャッジ時間 39,478 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 839 ms
64,328 KB
testcase_01 AC 842 ms
64,592 KB
testcase_02 AC 878 ms
64,696 KB
testcase_03 AC 1,067 ms
67,500 KB
testcase_04 AC 831 ms
64,448 KB
testcase_05 AC 829 ms
64,540 KB
testcase_06 AC 845 ms
64,496 KB
testcase_07 AC 979 ms
65,128 KB
testcase_08 AC 1,559 ms
66,200 KB
testcase_09 AC 3,521 ms
66,388 KB
testcase_10 AC 1,112 ms
66,072 KB
testcase_11 AC 1,895 ms
66,192 KB
testcase_12 AC 1,672 ms
73,736 KB
testcase_13 AC 1,596 ms
73,620 KB
testcase_14 AC 1,635 ms
73,744 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.math.{max, min}

def minDistance[T <: Iterable[(Int, Int)]](start: Int, graph: Array[T]): Array[Int] =
  val minDistance = Array.fill(graph.length){Int.MaxValue}
  minDistance(start) = 0
  val queue = mutable.PriorityQueue[(Int, Int)]((start, 0))(Ordering.by[(Int, Int), Int](_._2))
  while queue.nonEmpty do
    val (current, cost) = queue.dequeue()
    if minDistance(current) == cost then
      for (next, diff) <- graph(current) do
        if minDistance(next) > cost + diff then
          minDistance(next) = cost + diff
          queue.enqueue((next, cost + diff))
  minDistance

@main def main =
  val Array(n, m, k) = readLine().split(' ').map(_.toInt)
  val pigRoad = readLine().split(' ').map(_.toInt - 1)
  val edges = Array.fill(m){
    val Array(a, b, c) = readLine().split(' ').map(_.toInt)
    (a - 1, b - 1, c)
  }
  val graph = Array.fill(n){ArrayBuffer[(Int, Int)]()}
  for (a, b, c) <- edges do
    graph(a).append((b, c))
    graph(b).append((a, c))
  val pigVertices = pigRoad.map{i =>
    val (a, b, _) = edges(i)
    Array(a, b)
  }
  val fromStart = minDistance(0, graph)
  val fromGoal = minDistance(n - 1, graph)
  val minDistanceInPigRoad = Array.ofDim[Int](k, 2, k, 2)
  for i <- 0 until k do
    val (_, _, c) = edges(pigRoad(i))
    for j <- 0 until 2 do
      val start = pigVertices(i)(j)
      val distance = minDistance(start, graph)
      for
        ti <- 0 until k
        tj <- 0 until 2
      do
        minDistanceInPigRoad(i)(j)(ti)(tj) = distance(pigVertices(ti)(tj))
      minDistanceInPigRoad(i)(j)(i)(1 - j) = c
  val minCycle = Array.fill(1 << k){Array.fill(k){Array.fill(2){Int.MaxValue}}}
  for
    i <- 0 until k
    j <- 0 until 2
  do
    val v = pigVertices(i)(j)
    minCycle(0)(i)(j) = fromStart(v)
  for
    pattern <- minCycle.indices
    i <- 0 until k
    j <- 0 until 2
  do
    val currentDistance = minCycle(pattern)(i)(j) + edges(pigRoad(i))._3
    val nextPattern = pattern | (1 << i)
    minCycle(nextPattern)(i)(1 - j) = min(minCycle(nextPattern)(i)(1 - j), currentDistance)
    for
      nextI <- 0 until k
      nextJ <- 0 until 2
    do
      minCycle(nextPattern)(nextI)(nextJ) = min(minCycle(nextPattern)(nextI)(nextJ), currentDistance + minDistanceInPigRoad(i)(1 - j)(nextI)(nextJ))

  var result = Int.MaxValue
  for
    i <- 0 until k
    j <- 0 until 2
  do
    val currentDistance = minCycle.last(i)(j)
    val lastVertex = pigVertices(i)(j)
    val distance = currentDistance + fromGoal(lastVertex)
    result = min(result, distance)
  println(result)
0