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)