結果

問題 No.2805 Go to School
ユーザー suzuishisuzuishi
提出日時 2024-07-12 23:55:45
言語 Crystal
(1.11.2)
結果
AC  
実行時間 788 ms / 2,000 ms
コード長 1,839 bytes
コンパイル時間 13,991 ms
コンパイル使用メモリ 297,148 KB
実行使用メモリ 90,536 KB
最終ジャッジ日時 2024-07-16 01:41:55
合計ジャッジ時間 27,019 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 296 ms
73,304 KB
testcase_05 AC 482 ms
69,560 KB
testcase_06 AC 252 ms
34,480 KB
testcase_07 AC 214 ms
39,564 KB
testcase_08 AC 457 ms
66,736 KB
testcase_09 AC 240 ms
36,188 KB
testcase_10 AC 232 ms
42,160 KB
testcase_11 AC 704 ms
83,068 KB
testcase_12 AC 441 ms
57,100 KB
testcase_13 AC 715 ms
81,544 KB
testcase_14 AC 83 ms
16,996 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 371 ms
44,384 KB
testcase_19 AC 243 ms
30,616 KB
testcase_20 AC 655 ms
69,756 KB
testcase_21 AC 788 ms
79,892 KB
testcase_22 AC 360 ms
41,236 KB
testcase_23 AC 567 ms
58,924 KB
testcase_24 AC 552 ms
55,668 KB
testcase_25 AC 132 ms
28,216 KB
testcase_26 AC 537 ms
86,652 KB
testcase_27 AC 308 ms
62,176 KB
testcase_28 AC 23 ms
14,004 KB
testcase_29 AC 45 ms
20,448 KB
testcase_30 AC 165 ms
42,944 KB
testcase_31 AC 309 ms
38,520 KB
testcase_32 AC 504 ms
90,536 KB
testcase_33 AC 523 ms
85,964 KB
testcase_34 AC 3 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 255 ms
31,560 KB
testcase_37 AC 275 ms
38,684 KB
testcase_38 AC 437 ms
63,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BinaryHeap
  def initialize
    @data = Array(Int128).new
  end
  def size
    @data.size
  end
  def empty?
    @data.size == 0
  end
  def insert(x)
    idx = @data.size
    @data << x
    while idx > 0 && priority(@data[idx], @data[(idx - 1) >> 1])
      @data[(idx - 1) >> 1], @data[idx] = @data[idx], @data[(idx - 1) >> 1]
      idx = (idx - 1) >> 1
    end
  end
  def << (x)
    insert(x)
  end
  def pop
    return nil if @data.size == 0
    return @data.pop if @data.size == 1
    r = @data[0].dup
    @data[0] = @data.pop
    idx = 0
    while (n_idx = 2 * idx + 1) < @data.size
      n_idx += 1 if n_idx + 1 < @data.size && priority(@data[n_idx + 1], @data[n_idx])
      break if priority(@data[idx], @data[n_idx])
      @data[idx], @data[n_idx] = @data[n_idx], @data[idx]
      idx = n_idx
    end
    r
  end
  def top
    self.empty? ? nil : @data[0]
  end

  def priority(a, b) # return true iff a has higher priority than b
    (a >> 30) < (b >> 30)
  end
end

INF = 1_i64 << 60
Mask = (1 << 30) - 1
n, m, l, s, e = read_line.split.map &.to_i
g = Array.new(n << 1) {[] of Array(Int32)}
m.times do
  a, b, t = read_line.split.map &.to_i
  a -= 1
  b -= 1
  2.times do
    g[a] << [b, t]
    g[b] << [a, t]
    a += n
    b += n
  end
end
t = Array.new(n, false)
read_line.split.map(&.to_i).each do |i|
  t[i - 1] = true
end
q = BinaryHeap.new
q << 0_i128
dist = Array.new(n << 1, INF)
dist[0] = 0
while (ud = q.pop)
  u, d = ud & Mask, ud >> 30
  next if dist[u] < d
  g[u].each do |(v, c)|
    next if dist[v] <= dist[u] + c
    dist[v] = dist[u] + c
    q << (dist[v].to_i128 << 30 | v)
  end
  if u < n && t[u] && dist[u] < s + e && dist[u + n] > [dist[u] + 1, s + 1].max
    dist[u + n] = [dist[u] + 1, s.to_i64 + 1].max
    q << (dist[u + n].to_i128 << 30 | (u + n))
  end
end
puts dist[-1] == INF ? -1 : dist[-1]
0