結果

問題 No.1812 Uribo Road
ユーザー yudedakoyudedako
提出日時 2022-01-25 19:37:50
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 2,642 bytes
コンパイル時間 13,531 ms
コンパイル使用メモリ 260,924 KB
実行使用メモリ 72,764 KB
最終ジャッジ日時 2023-08-22 14:56:23
合計ジャッジ時間 46,006 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,003 ms
66,828 KB
testcase_01 AC 1,021 ms
63,588 KB
testcase_02 AC 1,040 ms
63,508 KB
testcase_03 AC 1,187 ms
63,608 KB
testcase_04 AC 1,009 ms
63,368 KB
testcase_05 AC 1,027 ms
63,336 KB
testcase_06 AC 1,023 ms
63,384 KB
testcase_07 AC 1,142 ms
63,628 KB
testcase_08 AC 2,008 ms
66,240 KB
testcase_09 AC 4,841 ms
66,196 KB
testcase_10 AC 1,417 ms
66,124 KB
testcase_11 AC 2,595 ms
66,036 KB
testcase_12 AC 2,008 ms
72,620 KB
testcase_13 AC 1,931 ms
72,764 KB
testcase_14 AC 1,961 ms
71,052 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