結果

問題 No.1607 Kth Maximum Card
ユーザー yakamotoyakamoto
提出日時 2021-07-16 23:17:07
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 2,359 ms / 3,500 ms
コード長 4,400 bytes
コンパイル時間 16,697 ms
コンパイル使用メモリ 432,608 KB
実行使用メモリ 100,900 KB
最終ジャッジ日時 2023-09-20 15:47:13
合計ジャッジ時間 55,328 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
53,952 KB
testcase_01 AC 279 ms
53,908 KB
testcase_02 AC 281 ms
53,956 KB
testcase_03 AC 282 ms
53,844 KB
testcase_04 AC 286 ms
53,804 KB
testcase_05 AC 281 ms
55,688 KB
testcase_06 AC 285 ms
53,852 KB
testcase_07 AC 279 ms
53,792 KB
testcase_08 AC 2,295 ms
98,260 KB
testcase_09 AC 1,139 ms
81,744 KB
testcase_10 AC 958 ms
95,944 KB
testcase_11 AC 639 ms
65,292 KB
testcase_12 AC 1,075 ms
81,224 KB
testcase_13 AC 825 ms
63,416 KB
testcase_14 AC 814 ms
65,444 KB
testcase_15 AC 1,799 ms
90,988 KB
testcase_16 AC 581 ms
61,388 KB
testcase_17 AC 577 ms
60,256 KB
testcase_18 AC 1,218 ms
76,704 KB
testcase_19 AC 987 ms
69,680 KB
testcase_20 AC 1,095 ms
71,616 KB
testcase_21 AC 1,176 ms
74,588 KB
testcase_22 AC 1,978 ms
100,788 KB
testcase_23 AC 2,150 ms
100,900 KB
testcase_24 AC 1,085 ms
70,376 KB
testcase_25 AC 853 ms
65,668 KB
testcase_26 AC 917 ms
67,764 KB
testcase_27 AC 1,061 ms
70,480 KB
testcase_28 AC 894 ms
67,524 KB
testcase_29 AC 849 ms
76,620 KB
testcase_30 AC 2,359 ms
98,172 KB
testcase_31 AC 1,377 ms
77,640 KB
testcase_32 AC 1,050 ms
76,488 KB
testcase_33 AC 1,114 ms
75,944 KB
testcase_34 AC 982 ms
76,812 KB
testcase_35 AC 1,090 ms
76,832 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:179:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:183:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:187:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:191:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:193:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:200:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:207:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:224:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
          ^

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.AssertionError
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

val MOD = 1_000_000_007

/**
 * 単調増加
 * (l, h] 方式
 * @return マッチするものがなかったらmx
 */
inline fun findMin(mn: Int, mx: Int, f: (Int) -> Boolean): Int {
  var low = mn - 1
  var high = mx
  while(high - low > 1) {
    val m = (low + high) / 2
    if (f(m)) high = m
    else low = m
  }
  return high
}
data class Edge(val v: Int, val u: Int, val c: Int, var weight: Int)
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  private val N = ni()
  private val M = ni()
  private val K = ni()
  private val D = IntArray(N)
  private val g = Array(N){ mutableListOf<Edge>()}
  val inf = 1e9.toInt() + 100
  fun dijk(g: Array<MutableList<Edge>>, s: Int): IntArray {
    D.fill(inf)
    D[s] = 0
    val que = ArrayDeque<Int>()
    que.add(s)
    while(que.isNotEmpty()) {
      val v = que.poll()!!

      for (i in 0 until g[v].size) {
        val e = g[v][i]
        if (D[v] + e.weight < D[e.u]) {
          D[e.u] = D[v] + e.weight
          if (e.weight == 0) que.addFirst(e.u) else que.addLast(e.u)
        }
      }
    }
    return D
  }

  fun solve() {
    for (i in 0 until M) {
      val (v, u) = na(2, -1)
      val c = ni()
      g[v].add(Edge(v, u, c, -1))
      g[u].add(Edge(u, v, c, -1))
    }

    fun calc(x: Int): IntArray {
      for (i in 0 until N) {
        for (j in g[i].indices) {
          val e = g[i][j]
          e.weight = if (e.c > x) 1 else 0
        }
      }

      return dijk(g, 0)
    }

    // K進むことなくNに到達できる
    if (calc(0)[N - 1] < K) {
      out.println(0)
      return
    }

    val ans = findMin(1, 200_000) { x ->
      val D = calc(x)
      D[N - 1] <= K - 1
    }

    out.println(ans)
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return IntArray(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

  private inline fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<BooleanArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0