結果
問題 | No.1308 ジャンプビーコン |
ユーザー | semisagi |
提出日時 | 2020-12-06 01:17:49 |
言語 | Swift (5.10.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,851 bytes |
コンパイル時間 | 7,619 ms |
コンパイル使用メモリ | 134,136 KB |
実行使用メモリ | 157,260 KB |
最終ジャッジ日時 | 2024-09-16 08:18:06 |
合計ジャッジ時間 | 50,145 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 8 ms
9,344 KB |
testcase_01 | AC | 9 ms
9,344 KB |
testcase_02 | AC | 9 ms
9,344 KB |
testcase_03 | AC | 9 ms
9,344 KB |
testcase_04 | AC | 9 ms
9,344 KB |
testcase_05 | AC | 9 ms
9,344 KB |
testcase_06 | AC | 8 ms
9,344 KB |
testcase_07 | AC | 8 ms
9,344 KB |
testcase_08 | AC | 12 ms
9,216 KB |
testcase_09 | AC | 12 ms
9,344 KB |
testcase_10 | AC | 12 ms
9,344 KB |
testcase_11 | AC | 12 ms
9,344 KB |
testcase_12 | AC | 12 ms
9,344 KB |
testcase_13 | AC | 224 ms
14,496 KB |
testcase_14 | AC | 223 ms
14,356 KB |
testcase_15 | AC | 226 ms
14,408 KB |
testcase_16 | AC | 227 ms
14,488 KB |
testcase_17 | AC | 223 ms
14,484 KB |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | TLE | - |
testcase_23 | AC | 8 ms
9,216 KB |
testcase_24 | AC | 12 ms
9,472 KB |
testcase_25 | AC | 12 ms
9,472 KB |
testcase_26 | TLE | - |
testcase_27 | TLE | - |
testcase_28 | TLE | - |
testcase_29 | TLE | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
ソースコード
struct Scanner { private var elements = [String]() private var index = 0 mutating func peek() -> String { while elements.count == index { elements = readLine()!.split(separator: " ").map(String.init) index = 0 } return elements[index] } mutating func next() -> String { defer { index += 1 } return peek() } mutating func nextInt() -> Int { return Int(next())! } mutating func nextInts(_ n: Int) -> [Int] { return (0 ..< n).map { _ in nextInt() } } mutating func nextDouble() -> Double { return Double(next())! } } struct Array2D<Element> { let n1: Int let n2: Int private var elements: [Element] init(repeating: Element, _ n1: Int, _ n2: Int) { self.n1 = n1 self.n2 = n2 self.elements = [Element](repeating: repeating, count: n1 * n2) } subscript(i: Int, j: Int) -> Element { get { elements[i * n2 + j] } set { elements[i * n2 + j] = newValue } } } var scanner = Scanner() let N = scanner.nextInt() let Q = scanner.nextInt() let C = scanner.nextInt() var tree = [[(Int, Int)]](repeating: [], count: N) for _ in 0 ..< N - 1 { let u = scanner.nextInt() - 1 let v = scanner.nextInt() - 1 let l = scanner.nextInt() tree[u].append((v, l)) tree[v].append((u, l)) } var distance = Array2D<Int>(repeating: 0, N, N) func dfs(u: Int, parent: Int?, root: Int) { for (v, w) in tree[u] where v != parent { distance[root, v] = distance[root, u] + w dfs(u: v, parent: u, root: root) } } for i in 0 ..< N { dfs(u: i, parent: nil, root: i) } let X = scanner.nextInts(Q).map { $0 - 1 } let inf = Int.max / 2 var dp = Array2D<Int>(repeating: inf, Q, N) dp[0, X[0]] = 0 extension RandomAccessCollection where Element == Int, Index == Int { func prefixMinimum(initialValue: Int) -> [Element] { var result = [Int](repeating: initialValue, count: count + 1) for i in indices { result[i + 1] = Swift.min(result[i], self[i]) } return result } func suffixMinimum(initialValue: Int) -> [Element] { var result = [Int](repeating: initialValue, count: count + 1) for i in indices.reversed() { result[i] = Swift.min(result[i + 1], self[i]) } return result } } for i in 0 ..< Q - 1 { var dp2 = [Int](repeating: inf, count: N) func dfs(_ u: Int, _ parent: Int?) { dp2[u] = dp[i, u] for (v, w) in tree[u] where v != parent { dfs(v, u) dp2[u] = min(dp2[u], dp2[v] + w) } } func dfs2(_ u: Int, _ parent: Int?) { let prefix = tree[u].lazy.map { (v, w) in dp2[v] + w }.prefixMinimum(initialValue: dp[i, u]) let suffix = tree[u].lazy.map { (v, w) in dp2[v] + w }.suffixMinimum(initialValue: dp[i, u]) dp[i + 1, u] = min(dp[i + 1, u], prefix.last! + C + distance[u, X[i + 1]]) for (i, (v, _)) in tree[u].enumerated() where v != parent { dp2[u] = min(prefix[i], suffix[i + 1]) dfs2(v, u) } } dfs(0, nil) dfs2(0, nil) for j in 0 ..< N where dp[i, j] != inf { dp[i + 1, j] = min(dp[i + 1, j], dp[i, j] + distance[X[i], X[i + 1]]) // for k in 0 ..< N { // dp[i + 1, k] = min(dp[i + 1, k], dp[i, j] + C + distance[j, k] + distance[k, X[i + 1]]) // dp[i + 1, k] = min(dp[i + 1, k], dp[i, j] + distance[X[i], k] + distance[k, X[i + 1]]) // } } let m = (0 ..< N).map { dp[i, $0] }.min()! for j in 0 ..< N { dp[i + 1, j] = min(dp[i + 1, j], m + distance[X[i], j] + distance[j, X[i + 1]]) } } print((0 ..< N).map { dp[Q - 1, $0] }.min()!)