結果

問題 No.1308 ジャンプビーコン
ユーザー semisagisemisagi
提出日時 2020-12-05 02:02:36
言語 Swift
(5.10.0)
結果
AC  
実行時間 1,388 ms / 4,000 ms
コード長 2,564 bytes
コンパイル時間 9,694 ms
コンパイル使用メモリ 130,408 KB
実行使用メモリ 150,656 KB
最終ジャッジ日時 2024-09-15 09:55:04
合計ジャッジ時間 28,911 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
9,344 KB
testcase_01 AC 9 ms
9,216 KB
testcase_02 AC 9 ms
9,088 KB
testcase_03 AC 9 ms
9,344 KB
testcase_04 AC 9 ms
9,088 KB
testcase_05 AC 9 ms
9,216 KB
testcase_06 AC 9 ms
9,216 KB
testcase_07 AC 9 ms
9,344 KB
testcase_08 AC 9 ms
9,344 KB
testcase_09 AC 9 ms
9,216 KB
testcase_10 AC 9 ms
9,216 KB
testcase_11 AC 10 ms
9,216 KB
testcase_12 AC 9 ms
9,344 KB
testcase_13 AC 41 ms
14,592 KB
testcase_14 AC 41 ms
14,720 KB
testcase_15 AC 41 ms
14,336 KB
testcase_16 AC 41 ms
14,592 KB
testcase_17 AC 41 ms
14,464 KB
testcase_18 AC 1,251 ms
150,272 KB
testcase_19 AC 1,238 ms
150,400 KB
testcase_20 AC 1,261 ms
150,400 KB
testcase_21 AC 1,245 ms
150,272 KB
testcase_22 AC 1,242 ms
150,400 KB
testcase_23 AC 9 ms
9,344 KB
testcase_24 AC 10 ms
9,344 KB
testcase_25 AC 10 ms
9,472 KB
testcase_26 AC 1,181 ms
150,272 KB
testcase_27 AC 1,172 ms
150,272 KB
testcase_28 AC 1,183 ms
150,272 KB
testcase_29 AC 1,295 ms
150,400 KB
testcase_30 AC 1,297 ms
150,656 KB
testcase_31 AC 1,073 ms
150,528 KB
testcase_32 AC 1,332 ms
150,656 KB
testcase_33 AC 1,388 ms
150,656 KB
testcase_34 AC 1,175 ms
150,272 KB
testcase_35 AC 1,174 ms
150,400 KB
testcase_36 AC 1,211 ms
150,272 KB
testcase_37 AC 1,209 ms
150,272 KB
testcase_38 AC 1,323 ms
150,272 KB
testcase_39 AC 1,320 ms
150,400 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.swift:103:5: warning: result of call to 'dfs' is unused
    dfs(X[i + 1], nil)
    ^  ~~~~~~~~~~~~~~~

ソースコード

diff #

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

for i in 0 ..< Q - 1 {
    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]])
    }
    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]])
    }
    func dfs(_ u: Int, _ parent: Int?) -> Int {
        var result = dp[i, u] + distance[u, X[i + 1]] + C
        for (v, _) in tree[u] where v != parent {
            let value = dfs(v, u)
            result = min(result, value)
        }
        dp[i + 1, u] = min(dp[i + 1, u], result)
        return result
    }
    dfs(X[i + 1], nil)
}

print((0 ..< N).map { dp[Q - 1, $0] }.min()!)
0