結果

問題 No.1442 I-wate Shortest Path Problem
ユーザー first_vilfirst_vil
提出日時 2021-03-09 20:37:36
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 2,699 ms / 3,000 ms
コード長 4,181 bytes
コンパイル時間 17,910 ms
コンパイル使用メモリ 454,628 KB
実行使用メモリ 135,196 KB
最終ジャッジ日時 2024-10-11 12:35:54
合計ジャッジ時間 61,636 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
49,868 KB
testcase_01 AC 277 ms
49,872 KB
testcase_02 AC 483 ms
60,840 KB
testcase_03 AC 815 ms
92,852 KB
testcase_04 AC 484 ms
61,292 KB
testcase_05 AC 446 ms
59,256 KB
testcase_06 AC 808 ms
95,620 KB
testcase_07 AC 466 ms
56,908 KB
testcase_08 AC 790 ms
96,772 KB
testcase_09 AC 537 ms
61,832 KB
testcase_10 AC 807 ms
95,552 KB
testcase_11 AC 817 ms
97,064 KB
testcase_12 AC 2,178 ms
122,344 KB
testcase_13 AC 1,240 ms
114,292 KB
testcase_14 AC 1,999 ms
121,940 KB
testcase_15 AC 1,883 ms
126,840 KB
testcase_16 AC 2,225 ms
126,692 KB
testcase_17 AC 2,619 ms
132,332 KB
testcase_18 AC 2,511 ms
129,984 KB
testcase_19 AC 2,378 ms
130,204 KB
testcase_20 AC 2,575 ms
130,332 KB
testcase_21 AC 2,699 ms
131,252 KB
testcase_22 AC 1,336 ms
116,732 KB
testcase_23 AC 2,309 ms
135,196 KB
testcase_24 AC 1,281 ms
112,968 KB
testcase_25 AC 2,192 ms
127,104 KB
testcase_26 AC 1,573 ms
122,144 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:75:13: warning: name shadowed: a
        var a = x
            ^
Main.kt:76:13: warning: name shadowed: b
        var b = y
            ^
Main.kt:138:10: warning: name shadowed: x
    for (x in ans) println(x)
         ^

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val k = 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] = nextInt() - 1
        b[i] = nextInt() - 1
        c[i] = 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] = nextInt()
        p[i] = nextInt()
        cnt[n + i] += m[i]
        x[i] = Array(m[i]) { 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 = nextInt()
    val ans = Array(q) { 0L }
    val u = Array(q) { 0 }
    val v = Array(q) { 0 }
    for (i in 0 until q) {
        u[i] = nextInt() - 1
        v[i] = 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()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()!!
fun nextDouble() = next().toDouble()
// endregion
0