結果

問題 No.1308 ジャンプビーコン
ユーザー semisagisemisagi
提出日時 2020-12-05 01:31:54
言語 Swift
(5.10.0)
結果
AC  
実行時間 3,971 ms / 4,000 ms
コード長 3,374 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 133,928 KB
実行使用メモリ 149,716 KB
最終ジャッジ日時 2023-10-13 12:59:31
合計ジャッジ時間 72,339 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,936 KB
testcase_01 AC 3 ms
7,892 KB
testcase_02 AC 2 ms
7,920 KB
testcase_03 AC 3 ms
7,888 KB
testcase_04 AC 3 ms
7,872 KB
testcase_05 AC 3 ms
7,924 KB
testcase_06 AC 3 ms
7,928 KB
testcase_07 AC 3 ms
7,900 KB
testcase_08 AC 5 ms
7,944 KB
testcase_09 AC 5 ms
7,980 KB
testcase_10 AC 6 ms
7,920 KB
testcase_11 AC 5 ms
8,096 KB
testcase_12 AC 5 ms
8,040 KB
testcase_13 AC 161 ms
13,116 KB
testcase_14 AC 163 ms
13,160 KB
testcase_15 AC 164 ms
13,028 KB
testcase_16 AC 165 ms
13,104 KB
testcase_17 AC 164 ms
13,004 KB
testcase_18 AC 3,851 ms
149,344 KB
testcase_19 AC 3,738 ms
149,356 KB
testcase_20 AC 3,827 ms
149,360 KB
testcase_21 AC 3,739 ms
149,520 KB
testcase_22 AC 3,727 ms
149,376 KB
testcase_23 AC 3 ms
7,928 KB
testcase_24 AC 5 ms
8,000 KB
testcase_25 AC 5 ms
8,048 KB
testcase_26 AC 3,243 ms
149,452 KB
testcase_27 AC 3,244 ms
149,532 KB
testcase_28 AC 3,403 ms
149,420 KB
testcase_29 AC 3,880 ms
149,548 KB
testcase_30 AC 3,799 ms
149,616 KB
testcase_31 AC 3,132 ms
149,476 KB
testcase_32 AC 3,861 ms
149,716 KB
testcase_33 AC 3,971 ms
149,636 KB
testcase_34 AC 3,330 ms
149,536 KB
testcase_35 AC 3,366 ms
149,528 KB
testcase_36 AC 3,233 ms
149,440 KB
testcase_37 AC 3,192 ms
149,464 KB
testcase_38 AC 3,897 ms
149,292 KB
testcase_39 AC 3,852 ms
149,412 KB
権限があれば一括ダウンロードができます

ソースコード

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

var prefix = [[Int]]()
var suffix = [[Int]]()

for i in 0 ..< N {
    prefix.append([Int](repeating: inf, count: tree[i].count + 1))
    suffix.append([Int](repeating: inf, count: tree[i].count + 1))
}

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?) {
        prefix[u][0] = dp[i, u]
        for (i, (v, w)) in tree[u].enumerated() {
            prefix[u][i + 1] = min(prefix[u][i], dp2[v] + w)
        }
        suffix[u][tree[u].count] = dp[i, u]
        for (i, (v, w)) in tree[u].enumerated().reversed() {
            suffix[u][i] = min(suffix[u][i + 1], dp2[v] + w)
        }
        dp[i + 1, u] = min(dp[i + 1, u], suffix[u][0] + C + distance[u, X[i + 1]])
        for i in tree[u].indices {
            let v = tree[u][i].0
            guard v != parent else { continue }
            dp2[u] = min(prefix[u][i], suffix[u][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]])
    }

    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()!)
0