結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | first_vil |
提出日時 | 2021-03-09 18:55:42 |
言語 | Kotlin (1.9.23) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,597 bytes |
コンパイル時間 | 17,426 ms |
コンパイル使用メモリ | 451,776 KB |
実行使用メモリ | 150,960 KB |
最終ジャッジ日時 | 2024-10-11 12:32:49 |
合計ジャッジ時間 | 60,663 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 291 ms
49,704 KB |
testcase_01 | AC | 293 ms
49,704 KB |
testcase_02 | AC | 494 ms
60,716 KB |
testcase_03 | AC | 824 ms
95,136 KB |
testcase_04 | AC | 513 ms
59,760 KB |
testcase_05 | AC | 461 ms
58,356 KB |
testcase_06 | AC | 746 ms
95,116 KB |
testcase_07 | AC | 480 ms
58,344 KB |
testcase_08 | AC | 773 ms
92,684 KB |
testcase_09 | AC | 554 ms
65,276 KB |
testcase_10 | AC | 806 ms
95,524 KB |
testcase_11 | AC | 820 ms
96,064 KB |
testcase_12 | AC | 2,264 ms
139,884 KB |
testcase_13 | AC | 1,215 ms
120,236 KB |
testcase_14 | AC | 1,972 ms
135,700 KB |
testcase_15 | AC | 2,012 ms
139,148 KB |
testcase_16 | AC | 2,234 ms
138,864 KB |
testcase_17 | AC | 2,792 ms
145,192 KB |
testcase_18 | TLE | - |
testcase_19 | AC | 2,423 ms
147,108 KB |
testcase_20 | AC | 2,719 ms
145,388 KB |
testcase_21 | AC | 2,924 ms
145,372 KB |
testcase_22 | AC | 1,462 ms
134,808 KB |
testcase_23 | AC | 2,600 ms
150,960 KB |
testcase_24 | AC | 1,284 ms
131,976 KB |
testcase_25 | AC | 2,357 ms
146,028 KB |
testcase_26 | AC | 1,564 ms
140,924 KB |
ソースコード
import java.io.PrintWriter import java.util.* fun PrintWriter.solve() { val n = nextInt() val k = nextInt() val g = Array(n + k) { arrayListOf<Pair<Int, Int>>() } repeat(n - 1) { val a = nextInt() - 1 val b = nextInt() - 1 val c = nextInt() g[a].add(Pair<Int, Int>(b, c)) g[b].add(Pair<Int, Int>(a, c)) } //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 (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 p = Array(n) { 0 } for (i in 0 until k) { val m = nextInt() p[i] = nextInt() repeat(m) { val x = nextInt() - 1 g[n + i].add(Pair<Int, Int>(x, 0)) g[x].add(Pair<Int, Int>(n + i, p[i])) } } 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