結果

問題 No.848 なかよし旅行
ユーザー pekempeypekempey
提出日時 2019-07-25 06:02:42
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,516 ms / 2,000 ms
コード長 1,585 bytes
コンパイル時間 15,996 ms
コンパイル使用メモリ 459,128 KB
実行使用メモリ 106,868 KB
最終ジャッジ日時 2024-04-25 13:59:43
合計ジャッジ時間 36,748 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,516 ms
106,868 KB
testcase_01 AC 331 ms
57,616 KB
testcase_02 AC 299 ms
55,400 KB
testcase_03 AC 325 ms
57,628 KB
testcase_04 AC 310 ms
57,692 KB
testcase_05 AC 317 ms
57,572 KB
testcase_06 AC 399 ms
58,044 KB
testcase_07 AC 340 ms
57,636 KB
testcase_08 AC 435 ms
57,944 KB
testcase_09 AC 455 ms
62,232 KB
testcase_10 AC 435 ms
58,004 KB
testcase_11 AC 880 ms
97,404 KB
testcase_12 AC 986 ms
101,256 KB
testcase_13 AC 1,150 ms
101,568 KB
testcase_14 AC 665 ms
90,432 KB
testcase_15 AC 1,058 ms
101,804 KB
testcase_16 AC 1,333 ms
105,868 KB
testcase_17 AC 1,067 ms
100,524 KB
testcase_18 AC 874 ms
97,920 KB
testcase_19 AC 901 ms
94,612 KB
testcase_20 AC 579 ms
65,400 KB
testcase_21 AC 1,213 ms
103,368 KB
testcase_22 AC 1,159 ms
102,692 KB
testcase_23 AC 553 ms
63,324 KB
testcase_24 AC 321 ms
55,292 KB
testcase_25 AC 1,302 ms
106,856 KB
testcase_26 AC 310 ms
55,360 KB
testcase_27 AC 319 ms
55,232 KB
testcase_28 AC 312 ms
55,336 KB
testcase_29 AC 313 ms
55,504 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:33:10: warning: parameter 'args' is never used
fun main(args: Array<String>) {
         ^

ソースコード

diff #

import java.util.PriorityQueue
import java.util.Scanner

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>) {
  val sc = Scanner(System.`in`)
  val N = sc.nextInt()
  val M = sc.nextInt()
  val P = sc.nextInt() - 1
  val Q = sc.nextInt() - 1
  val T = sc.nextLong()
  var g = Array(N, {mutableListOf<Edge>()})
  repeat(M) {
    val u = sc.nextInt() - 1
    val v = sc.nextInt() - 1
    val w = sc.nextLong()
    g[u].add(Edge(v, w))
    g[v].add(Edge(u, w))
  }
  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)
  }
}
0