結果

問題 No.848 なかよし旅行
ユーザー pekempeypekempey
提出日時 2019-07-25 06:53:47
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 847 ms / 2,000 ms
コード長 2,143 bytes
コンパイル時間 14,744 ms
コンパイル使用メモリ 458,932 KB
実行使用メモリ 95,240 KB
最終ジャッジ日時 2024-04-25 14:00:27
合計ジャッジ時間 30,098 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 847 ms
95,240 KB
testcase_01 AC 298 ms
56,364 KB
testcase_02 AC 287 ms
56,200 KB
testcase_03 AC 296 ms
56,440 KB
testcase_04 AC 295 ms
56,444 KB
testcase_05 AC 296 ms
56,324 KB
testcase_06 AC 302 ms
56,396 KB
testcase_07 AC 297 ms
56,524 KB
testcase_08 AC 328 ms
56,564 KB
testcase_09 AC 333 ms
56,576 KB
testcase_10 AC 325 ms
56,608 KB
testcase_11 AC 531 ms
66,100 KB
testcase_12 AC 554 ms
67,896 KB
testcase_13 AC 594 ms
72,416 KB
testcase_14 AC 458 ms
61,208 KB
testcase_15 AC 603 ms
70,280 KB
testcase_16 AC 745 ms
80,784 KB
testcase_17 AC 636 ms
70,772 KB
testcase_18 AC 528 ms
64,616 KB
testcase_19 AC 542 ms
65,996 KB
testcase_20 AC 405 ms
58,868 KB
testcase_21 AC 647 ms
75,448 KB
testcase_22 AC 582 ms
75,496 KB
testcase_23 AC 407 ms
58,904 KB
testcase_24 AC 289 ms
56,312 KB
testcase_25 AC 752 ms
80,696 KB
testcase_26 AC 284 ms
56,412 KB
testcase_27 AC 288 ms
56,276 KB
testcase_28 AC 278 ms
56,208 KB
testcase_29 AC 287 ms
56,428 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:35:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^
Main.kt:42:15: warning: 'toInt(): Int' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
      if ('0'.toInt() <= c) break
              ^
Main.kt:44:19: warning: 'toLong(): Long' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
    ans = c - '0'.toLong()
                  ^
Main.kt:47:15: warning: 'toInt(): Int' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
      if ('0'.toInt() > c) break
              ^
Main.kt:48:33: warning: 'toLong(): Long' is deprecated. Conversion of Char to Number is deprecated. Use Char.code property instead.
      ans = ans * 10 + (c - '0'.toLong())
                                ^

ソースコード

diff #

import java.util.PriorityQueue
import java.util.Scanner
import java.io.BufferedReader
import java.io.InputStreamReader

data class Edge(val v: Int, val w: Long)

val INF = 1e18.toLong()

fun dijkstra(g: Array<MutableList<Edge>>, s: Int): Array<Long> {
  data class State(val d: Long, val u: Int) : Comparable<State> {
    override operator fun compareTo(other: State): Int {
      if (d != other.d) return d.compareTo(other.d)
      return u.compareTo(other.u)
    }
  }
  var q: PriorityQueue<State> = PriorityQueue()
  val n = g.size
  var dist = Array(n, {INF})
  dist[s] = 0
  q.add(State(0, s))
  while (!q.isEmpty()) {
    val (d, u) = q.remove()
    if (d > dist[u]) continue
    for (e in g[u]) {
      if (dist[e.v] > dist[u] + e.w) {
        dist[e.v] = dist[u] + e.w
        q.add(State(dist[e.v], e.v))
      }
    }
  }
  return dist
}

fun main(args: Array<String>) {
  var br = BufferedReader(InputStreamReader(System.`in`), 1000000)
  val readLong = fun(): Long {
    var ans: Long
    var c: Int
    while (true) {
      c = br.read()
      if ('0'.toInt() <= c) break
    }
    ans = c - '0'.toLong()
    while (true) {
      c = br.read()
      if ('0'.toInt() > c) break
      ans = ans * 10 + (c - '0'.toLong())
    }
    return ans
  }
  val readInt = fun(): Int = readLong().toInt()
  val start = System.currentTimeMillis()
  val N = readInt()
  val M = readInt()
  val P = readInt() - 1
  val Q = readInt() - 1
  val T = readLong()
  var g = Array(N, {mutableListOf<Edge>()})
  repeat(M) {
    val u = readInt() - 1
    val v = readInt() - 1
    val w = readLong()
    g[u].add(Edge(v, w))
    g[v].add(Edge(u, w))
  }
  val end = System.currentTimeMillis()
  System.err.println(end - start)
  val d0 = dijkstra(g, 0)
  val dp = dijkstra(g, P)
  val dq = dijkstra(g, Q)
  var ans = -INF
  if (dp[0] + dp[Q] + dq[0] <= T) {
    ans = T
  }
  for (i in 0..N-1) {
    for (j in 0..N-1) {
      if (d0[i] + d0[j] + maxOf(dp[i] + dp[j], dq[i] + dq[j]) <= T) {
        ans = maxOf(ans, T - maxOf(dp[i] + dp[j], dq[i] + dq[j]))
      }
    }
  }
  if (ans < 0L) {
    println(-1)
  } else {
    println(ans)
  }
  br.close()
}
0