結果

問題 No.1473 おでぶなおばけさん
ユーザー 👑 箱
提出日時 2021-07-03 20:44:37
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,252 ms / 2,000 ms
コード長 2,601 bytes
コンパイル時間 18,095 ms
コンパイル使用メモリ 420,036 KB
実行使用メモリ 90,412 KB
最終ジャッジ日時 2023-09-13 02:17:22
合計ジャッジ時間 61,724 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 292 ms
52,624 KB
testcase_01 AC 296 ms
52,512 KB
testcase_02 AC 1,036 ms
78,100 KB
testcase_03 AC 920 ms
71,728 KB
testcase_04 AC 729 ms
62,820 KB
testcase_05 AC 547 ms
58,552 KB
testcase_06 AC 892 ms
69,752 KB
testcase_07 AC 1,188 ms
90,412 KB
testcase_08 AC 1,252 ms
90,200 KB
testcase_09 AC 1,182 ms
90,120 KB
testcase_10 AC 856 ms
62,988 KB
testcase_11 AC 849 ms
63,092 KB
testcase_12 AC 830 ms
63,104 KB
testcase_13 AC 719 ms
60,952 KB
testcase_14 AC 643 ms
58,840 KB
testcase_15 AC 792 ms
62,720 KB
testcase_16 AC 818 ms
63,056 KB
testcase_17 AC 446 ms
58,008 KB
testcase_18 AC 481 ms
58,744 KB
testcase_19 AC 687 ms
62,900 KB
testcase_20 AC 839 ms
66,964 KB
testcase_21 AC 928 ms
69,076 KB
testcase_22 AC 726 ms
70,996 KB
testcase_23 AC 685 ms
66,724 KB
testcase_24 AC 680 ms
66,908 KB
testcase_25 AC 742 ms
70,844 KB
testcase_26 AC 714 ms
68,888 KB
testcase_27 AC 549 ms
60,324 KB
testcase_28 AC 913 ms
77,732 KB
testcase_29 AC 751 ms
64,716 KB
testcase_30 AC 796 ms
66,928 KB
testcase_31 AC 784 ms
77,332 KB
testcase_32 AC 678 ms
69,116 KB
testcase_33 AC 652 ms
66,636 KB
testcase_34 AC 545 ms
60,272 KB
testcase_35 AC 560 ms
64,636 KB
testcase_36 AC 654 ms
64,740 KB
testcase_37 AC 654 ms
66,644 KB
testcase_38 AC 486 ms
58,292 KB
testcase_39 AC 938 ms
63,348 KB
testcase_40 AC 911 ms
62,940 KB
testcase_41 AC 885 ms
63,100 KB
testcase_42 AC 895 ms
62,952 KB
testcase_43 AC 717 ms
69,068 KB
testcase_44 AC 701 ms
68,908 KB
testcase_45 AC 705 ms
68,844 KB
testcase_46 AC 880 ms
71,944 KB
testcase_47 AC 1,016 ms
73,416 KB
testcase_48 AC 910 ms
73,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

fun PrintWriter.solve() {
    val n = nextInt()
    val m = nextInt()
    val data = mutableListOf<Triple<Int, Int, Int>>()
    val adj = Array(n) { mutableListOf<Int>() }
    val uf = UnionFind(n)
    for (i in 0 until m) {
        val s = nextInt() - 1
        val t = nextInt() - 1
        val d = nextInt()
        data.add(Triple(s, t, d))
    }
    data.sortByDescending { it.third }
    for ((s, t, d) in data) {
        uf.unite(s, t)
        if (uf.same(0, n - 1)) {
            for ((s2, t2, d2) in data) {
                if (d2 >= d) {
                    adj[s2].add(t2)
                    adj[t2].add(s2)
                }
            }
            val que: Queue<Pair<Int, Int>> = ArrayDeque()
            que.add(0 to 0)
            val visited = BooleanArray(n) { false }
            visited[0] = true
            while (que.isNotEmpty()) {
                val (v, num) = que.poll()
                if (v == n - 1) {
                    println("$d $num")
                    return
                }
                for (w in adj[v]) {
                    if (!visited[w]) {
                        que.add(w to num + 1)
                        visited[w] = true
                    }
                }
            }
        }
    }
}

class UnionFind(n: Int) {
    private val rank = IntArray(n) { 0 }
    private val parent = IntArray(n) { -1 }

    fun find(x: Int): Int {
        if (parent[x] < 0) {
            return x
        }
        parent[x] = find(parent[x])
        return parent[x]
    }

    fun unite(x: Int, y: Int) {
        val x1 = find(x)
        val y1 = find(y)
        if (x1 == y1) {
            return
        }
        if (rank[x1] < rank[y1]) {
            parent[y1] += parent[x1]
            parent[x1] = y1
        } else {
            parent[x1] += parent[y1]
            parent[y1] = x1
            if (rank[x1] == rank[y1]) {
                rank[x1]++
            }
        }
    }

    fun same(x: Int, y: Int): Boolean {
        return find(x) == find(y)
    }

    fun size(x: Int): Int {
        return -parent[find(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