結果
問題 | No.1833 Subway Planning |
ユーザー | yudedako |
提出日時 | 2022-02-22 12:11:29 |
言語 | Scala(Beta) (3.4.0) |
結果 |
AC
|
実行時間 | 2,796 ms / 4,000 ms |
コード長 | 1,972 bytes |
コンパイル時間 | 13,140 ms |
コンパイル使用メモリ | 269,944 KB |
実行使用メモリ | 149,644 KB |
最終ジャッジ日時 | 2024-06-30 13:18:43 |
合計ジャッジ時間 | 69,434 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 943 ms
64,132 KB |
testcase_01 | AC | 956 ms
64,012 KB |
testcase_02 | AC | 957 ms
63,972 KB |
testcase_03 | AC | 958 ms
63,780 KB |
testcase_04 | AC | 1,935 ms
138,272 KB |
testcase_05 | AC | 1,966 ms
138,580 KB |
testcase_06 | AC | 2,671 ms
138,484 KB |
testcase_07 | AC | 2,784 ms
138,548 KB |
testcase_08 | AC | 2,796 ms
138,596 KB |
testcase_09 | AC | 2,081 ms
134,896 KB |
testcase_10 | AC | 2,679 ms
149,644 KB |
testcase_11 | AC | 2,324 ms
139,896 KB |
testcase_12 | AC | 2,061 ms
136,036 KB |
testcase_13 | AC | 2,716 ms
144,912 KB |
testcase_14 | AC | 2,469 ms
138,704 KB |
testcase_15 | AC | 2,015 ms
138,316 KB |
testcase_16 | AC | 2,622 ms
138,836 KB |
testcase_17 | AC | 2,700 ms
138,308 KB |
testcase_18 | AC | 2,723 ms
143,672 KB |
testcase_19 | AC | 2,085 ms
138,348 KB |
testcase_20 | AC | 1,979 ms
116,924 KB |
testcase_21 | AC | 2,760 ms
138,300 KB |
testcase_22 | AC | 2,719 ms
135,996 KB |
testcase_23 | AC | 946 ms
63,852 KB |
testcase_24 | AC | 939 ms
63,864 KB |
testcase_25 | AC | 947 ms
64,132 KB |
ソースコード
import scala.collection.mutable.* import scala.io.StdIn.* import scala.util.chaining.* import scala.math.* import scala.reflect.ClassTag import scala.util.* import scala.annotation.tailrec import scala.collection.mutable @main def main = val n = readLine.toInt val edges = Array.fill(n - 1){ 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).addOne((b, c)) graph(b).addOne((a, c)) val sortedEdge = edges.sortBy(-_._3) val root = sortedEdge.head._1 val depth = Array.fill(n){-1}.tap(_(root) = 0) val parent = Array.fill(n){-1} val cost = Array.fill(n){-1} val stack = mutable.Stack[Int](root) while stack.nonEmpty do val top = stack.pop() for (next, c) <- graph(top) do if depth(next) == -1 then depth(next) = depth(top) + 1 parent(next) = top cost(next) = c stack.push(next) val degree = Array.fill(n){0} var canReduceAll = true var currentMin = Int.MaxValue val onPath = Array.fill(n){false}.tap(_(root) = true) var result = sortedEdge(0)._3 for i <- sortedEdge.indices do if canReduceAll then val (a, b, _) = sortedEdge(i) val nextCost = if sortedEdge.indices.contains(i + 1) then sortedEdge(i + 1)._3 else 0 val start = if depth(a) < depth(b) then b else a if !onPath(start) then var current = start while !onPath(current) do currentMin = min(currentMin, cost(current)) onPath(current) = true current = parent(current) degree(current) += 1 if (current != root && degree(current) > 1) || (current == root && degree(current) > 2) then canReduceAll = false else result = min(result, max(nextCost, sortedEdge.head._3 - currentMin)) else result = min(result, max(nextCost, sortedEdge.head._3 - currentMin)) println(result)