結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー first_vilfirst_vil
提出日時 2021-03-11 03:29:51
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 2,174 ms / 3,000 ms
コード長 4,928 bytes
コンパイル時間 21,248 ms
コンパイル使用メモリ 470,756 KB
実行使用メモリ 127,516 KB
最終ジャッジ日時 2024-10-12 18:25:34
合計ジャッジ時間 54,713 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 291 ms
54,308 KB
testcase_01 AC 286 ms
54,044 KB
testcase_02 AC 450 ms
62,288 KB
testcase_03 AC 663 ms
78,960 KB
testcase_04 AC 448 ms
60,940 KB
testcase_05 AC 369 ms
58,388 KB
testcase_06 AC 661 ms
78,020 KB
testcase_07 AC 426 ms
61,176 KB
testcase_08 AC 631 ms
76,496 KB
testcase_09 AC 500 ms
63,968 KB
testcase_10 AC 740 ms
80,820 KB
testcase_11 AC 694 ms
77,136 KB
testcase_12 AC 2,004 ms
126,924 KB
testcase_13 AC 893 ms
105,164 KB
testcase_14 AC 1,559 ms
123,412 KB
testcase_15 AC 1,372 ms
108,608 KB
testcase_16 AC 1,728 ms
117,408 KB
testcase_17 AC 2,101 ms
120,388 KB
testcase_18 AC 2,095 ms
118,668 KB
testcase_19 AC 1,951 ms
124,672 KB
testcase_20 AC 2,174 ms
119,880 KB
testcase_21 AC 2,014 ms
121,408 KB
testcase_22 AC 878 ms
106,140 KB
testcase_23 AC 2,064 ms
127,516 KB
testcase_24 AC 885 ms
104,528 KB
testcase_25 AC 1,982 ms
117,588 KB
testcase_26 AC 1,154 ms
107,692 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:132:10: warning: name shadowed: x
    for (x in ans) println(x)
         ^
Main.kt:179:24: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b < '0'.toByte() || b > '9'.toByte()) b = readByte()
                       ^
Main.kt:179:44: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while (b < '0'.toByte() || b > '9'.toByte()) b = readByte()
                                           ^
Main.kt:180:20: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
                   ^
Main.kt:180:46: warning: 'toByte(): Byte' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
                                             ^
Main.kt:182:26: 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 = IntArray(n - 1)
    val b = IntArray(n - 1)
    val c = IntArray(n - 1)
    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 = IntArray(k)
    val p = IntArray(k)
    val x = Array(k) {IntArray(0)}
    for (i in 0 until k) {
        m[i] = scan.nextInt()
        p[i] = scan.nextInt()
        cnt[n + i] += m[i]
        x[i] = IntArray(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 pq = PriorityQueue<Pair<Long, Int>>(compareBy {it.first})
    for (i in 0 until k) {
        val dp = LongArray(n + k) { 1000000000000000000L }
        dp[n + i] = 0
        pq.add(Pair<Long,Int>(0L,n + i))
        while (pq.isNotEmpty()) {
            val (d,cur) = pq.remove()
            if (dp[cur] < d) continue
            for ((to, cost) in g[cur]) {
                if (dp[to] > d + cost) {
                    dp[to] = d + cost
                    pq.add(Pair<Long,Int>(d + cost, 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(65536)
    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 {
        val sb = StringBuilder()
        var b = readByte()
        while (b in printable) {
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }
    fun nextInt(): Int {
        var n = 0
        var b = readByte()
        
        while (b < '0'.toByte() || b > '9'.toByte()) b = readByte()
        while ('0'.toByte() <= b && b <= '9'.toByte()) {
            n *= 10
            n += b - '0'.toByte()
            b = readByte()
        }
        return n
    }
    fun nextDouble(): Double {
        return next().toDouble()
    }
}
0