結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー first_vilfirst_vil
提出日時 2021-03-09 20:44:38
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 2,437 ms / 3,000 ms
コード長 5,796 bytes
コンパイル時間 20,064 ms
コンパイル使用メモリ 474,336 KB
実行使用メモリ 127,568 KB
最終ジャッジ日時 2024-04-20 09:36:22
合計ジャッジ時間 56,243 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
53,096 KB
testcase_01 AC 291 ms
54,508 KB
testcase_02 AC 495 ms
61,304 KB
testcase_03 AC 759 ms
75,548 KB
testcase_04 AC 453 ms
57,756 KB
testcase_05 AC 412 ms
53,768 KB
testcase_06 AC 770 ms
75,216 KB
testcase_07 AC 439 ms
56,832 KB
testcase_08 AC 701 ms
73,392 KB
testcase_09 AC 539 ms
59,548 KB
testcase_10 AC 821 ms
78,244 KB
testcase_11 AC 758 ms
73,964 KB
testcase_12 AC 2,075 ms
127,568 KB
testcase_13 AC 1,046 ms
107,040 KB
testcase_14 AC 1,667 ms
122,104 KB
testcase_15 AC 1,484 ms
108,728 KB
testcase_16 AC 1,997 ms
122,860 KB
testcase_17 AC 2,338 ms
125,712 KB
testcase_18 AC 2,319 ms
123,280 KB
testcase_19 AC 1,981 ms
122,536 KB
testcase_20 AC 2,437 ms
125,700 KB
testcase_21 AC 2,379 ms
127,320 KB
testcase_22 AC 1,107 ms
107,160 KB
testcase_23 AC 2,139 ms
126,508 KB
testcase_24 AC 1,040 ms
106,668 KB
testcase_25 AC 2,165 ms
120,320 KB
testcase_26 AC 1,317 ms
109,256 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:77:13: warning: name shadowed: a
        var a = x
            ^
Main.kt:78:13: warning: name shadowed: b
        var b = y
            ^
Main.kt:140:10: warning: name shadowed: x
    for (x in ans) println(x)
         ^
Main.kt:189:22: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        if (b == '-'.toByte()) {
                     ^
Main.kt:193:23: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        if (b !in '0'.toByte()..'9'.toByte()) {
                      ^
Main.kt:193:37: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        if (b !in '0'.toByte()..'9'.toByte()) {
                                    ^
Main.kt:197:26: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
            if (b in '0'.toByte()..'9'.toByte()) {
                         ^
Main.kt:197:40: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
            if (b in '0'.toByte()..'9'.toByte()) {
                                       ^
Main.kt:199:30: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
                n += b - '0'.toByte()
                             ^

ソースコード

diff #

import java.io.PrintWriter
import java.util.*

fun PrintWriter.solve() {
    val scan=FastScanner()

    val n = scan.nextInt()
    val k = scan.nextInt()

    //Making Graph-begin
    val cnt = IntArray(n + k)
    val a = Array(n - 1) { 0 }
    val b = Array(n - 1) { 0 }
    val c = Array(n - 1) { 0 }
    for (i in 0 until n - 1) {
        a[i] = scan.nextInt() - 1
        b[i] = scan.nextInt() - 1
        c[i] = scan.nextInt()
        ++cnt[a[i]]
        ++cnt[b[i]]
    }

    val m = Array(k) { 0 }
    val p = Array(k) { 0 }
    val x = Array(k) { Array(0) { 0 } }
    for (i in 0 until k) {
        m[i] = scan.nextInt()
        p[i] = scan.nextInt()
        cnt[n + i] += m[i]
        x[i] = Array(m[i]) { scan.nextInt() - 1 }
        for (j in x[i]) ++cnt[j]
    }

    val g = Array(n + k) { Array(0) { Pair<Int, Int>(0, 0) } }
    for (i in 0 until n + k) g[i] = Array(cnt[i]) { Pair<Int, Int>(0, 0) }
    for (i in 0 until n - 1) {
        g[a[i]][--cnt[a[i]]] = Pair<Int, Int>(b[i], c[i])
        g[b[i]][--cnt[b[i]]] = Pair<Int, Int>(a[i], c[i])
    }
    for (i in 0 until k) {
        for (j in x[i]) {
            g[n + i][--cnt[n + i]] = Pair<Int, Int>(j, 0)
            g[j][--cnt[j]] = Pair<Int, Int>(n + i, p[i])
        }
    }
    //Making Graph-end

    //LCA-begin
    val logn = 16
    val dep = Array(n) { -1 }
    val dis = LongArray(n) { 0 }
    val nxt = Array(logn + 1) { Array(n) { -1 } }
    val que = ArrayDeque<Int>()
    dep[0] = 0
    que.addLast(0)
    while (que.isNotEmpty()) {
        val cur = que.pollFirst()
        for ((to, cost) in g[cur]) {
            if (to < n && dep[to] == -1) {
                dep[to] = dep[cur] + 1
                dis[to] = dis[cur] + cost
                nxt[0][to] = cur
                que.addLast(to)
            }
        }
    }

    for (j in 0 until logn) {
        for (i in 0 until n) {
            if (nxt[j][i] != -1) {
                nxt[j + 1][i] = nxt[j][nxt[j][i]]
            }
        }
    }

    fun lca(x: Int, y: Int): Int {
        var a = x
        var b = y
        if (dep[a] > dep[b]) {
            a = y
            b = x
        }
        val d = dep[b] - dep[a]
        for (j in logn downTo 0) {
            if (d and (1 shl j) > 0) b = nxt[j][b]
        }
        if (a == b) return a
        for (j in logn downTo 0) {
            if (nxt[j][a] != nxt[j][b]) {
                a = nxt[j][a]
                b = nxt[j][b]
            }
        }
        return nxt[0][a]
    }
    //LCA-end

    val q = scan.nextInt()
    val ans = Array(q) { 0L }
    val u = Array(q) { 0 }
    val v = Array(q) { 0 }
    for (i in 0 until q) {
        u[i] = scan.nextInt() - 1
        v[i] = scan.nextInt() - 1
        ans[i] = dis[u[i]] + dis[v[i]] - 2 * dis[lca(u[i], v[i])]
    }

    //Dijkstra-begin
    val dp = Array(n + k) { 0L }
    val pq = PriorityQueue<Long>()
    for (i in 0 until k) {
        val yet = Array(n + k) { true }
        dp[n + i] = 0
        yet[n + i] = false
        pq.add((n + i).toLong())
        while (pq.isNotEmpty()) {
            var d = pq.remove()
            val cur = (d % (n + k)).toInt()
            d /= n + k
            if (dp[cur] < d) continue
            for ((to, cost) in g[cur]) {
                if (yet[to]) {
                    yet[to] = false
                    dp[to] = d + cost
                    pq.add((d + cost) * (n + k) + to)
                } else if (dp[to] > d + cost) {
                    dp[to] = d + cost
                    pq.add((d + cost) * (n + k) + to)
                }
            }
        }
        for (j in 0 until q) {
            if (ans[j] > dp[u[j]] + dp[v[j]] + p[i]) {
                ans[j] = dp[u[j]] + dp[v[j]] + p[i]
            }
        }
    }
    //Dijkstra-end

    for (x in ans) println(x)
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve()
    writer.flush()
}

private class FastScanner {
    private val printable = 33..126
    val `in` = System.`in`
    val buffer = ByteArray(1024)
    var ptr = 0
    var buflen = 0
    fun hasNextByte(): Boolean {
        if (ptr < buflen) {
            return true
        } else {
            ptr = 0
            buflen = `in`.read(buffer)
            if (buflen <= 0) {
                return false
            }
        }
        return true
    }
    fun readByte(): Byte {
        return if (hasNextByte()) buffer[ptr++] else return -1
    }
    fun hasNext(): Boolean {
        while (hasNextByte() && buffer[ptr] !in printable) ptr++
        return hasNextByte()
    }
    fun next(): String {
        if (!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while (b in printable) {
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }
    fun nextLong(): Long {
        if (!hasNext()) throw NoSuchElementException()
        var n = 0L
        var minus = false
        var b = readByte()
        if (b == '-'.toByte()) {
            minus = true
            b = readByte()
        }
        if (b !in '0'.toByte()..'9'.toByte()) {
            throw NumberFormatException()
        }
        while (true) {
            if (b in '0'.toByte()..'9'.toByte()) {
                n *= 10
                n += b - '0'.toByte()
            } else if (b == (-1).toByte() || b !in printable) {
                return if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }
    fun nextInt(): Int {
        val nl = nextLong()
        if (nl !in Integer.MIN_VALUE..Integer.MAX_VALUE) throw NumberFormatException()
        return nl.toInt()
    }
    fun nextDouble(): Double {
        return next().toDouble()
    }
}
0