結果

問題 No.1812 Uribo Road
ユーザー yudedakoyudedako
提出日時 2022-01-25 19:37:50
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 2,642 bytes
コンパイル時間 14,935 ms
コンパイル使用メモリ 274,212 KB
実行使用メモリ 145,144 KB
最終ジャッジ日時 2024-12-17 14:08:54
合計ジャッジ時間 123,869 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,002 ms
69,684 KB
testcase_01 AC 1,006 ms
69,648 KB
testcase_02 AC 1,017 ms
73,084 KB
testcase_03 AC 1,258 ms
141,944 KB
testcase_04 AC 1,012 ms
69,824 KB
testcase_05 AC 1,012 ms
141,228 KB
testcase_06 AC 1,027 ms
69,808 KB
testcase_07 AC 1,191 ms
141,600 KB
testcase_08 AC 1,896 ms
71,068 KB
testcase_09 AC 4,261 ms
142,384 KB
testcase_10 AC 1,341 ms
71,344 KB
testcase_11 AC 2,291 ms
140,500 KB
testcase_12 AC 2,003 ms
79,072 KB
testcase_13 AC 1,924 ms
145,144 KB
testcase_14 AC 1,958 ms
78,776 KB
testcase_15 TLE -
testcase_16 AC 1,887 ms
77,252 KB
testcase_17 TLE -
testcase_18 AC 4,944 ms
75,692 KB
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 1,594 ms
67,660 KB
testcase_24 AC 1,204 ms
70,912 KB
testcase_25 TLE -
testcase_26 AC 2,916 ms
67,336 KB
testcase_27 TLE -
testcase_28 TLE -
testcase_29 AC 1,028 ms
66,460 KB
testcase_30 AC 3,148 ms
67,168 KB
testcase_31 TLE -
testcase_32 AC 1,395 ms
67,308 KB
testcase_33 AC 4,440 ms
144,984 KB
権限があれば一括ダウンロードができます

ソースコード

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