Submission #13001205


Source Code Expand

class PriorityQueue
  def initialize
    @heap = []
  end

  def push value
    @heap.push value
    update_heap_to_up
  end

  def empty?
    @heap.empty?
  end

  def shift
    min, last = @heap[0], @heap.pop
    unless @heap.empty?
      @heap[0] = last
      update_heap_to_down
    end
    min
  end

  alias pop shift

  private

  def update_heap_to_up
    index    = @heap.size - 1
    up_value = @heap[index]
    while 0 <= (parent_index = (index - 1) / 2)
      parent_value = @heap[parent_index]
      break if parent_value <= up_value

      @heap[index] = parent_value
      index        = parent_index
    end
    @heap[index] = up_value
  end

  def update_heap_to_down
    index      = 0
    down_value = @heap[0]
    size       = @heap.size
    while (child_index = 2 * index + 1) < size
      # 右の子がいて、右の子が小さければ、右の子を使う
      child_index += 1 if child_index + 1 < size and @heap[child_index + 1] < @heap[child_index]
      child_value = @heap[child_index]
      break if down_value <= child_value

      @heap[index] = child_value
      index        = child_index
    end
    @heap[index] = down_value
  end
end

MAX_V = 50 # 制約の最大値。
MAX_S = MAX_V * 50 + 5
INF   = Float::INFINITY

# 頂点
Edge = Struct.new(:to, :a, :b)
# 両替所
Exchange = Struct.new(:v, :s, :x) do
  include Comparable
  def <=>(other)
    self.x <=> other.x
  end
end

g    = Array.new(MAX_V) { Array.new { Edge.new } }
dist = Array.new(MAX_V) { Array.new(MAX_S + 5) { INF } }

def input
  gets.split.map &:to_i;
end

# メイン
n, m, s = input
# 都市の入力
m.times do
  u, v, a, b = input
  u          = u.pred
  v          = v.pred
  g[u] << Edge.new(v, a, b)
  g[v] << Edge.new(u, a, b)
end

# プライオリティキューのライブラリを利用
# require 'pqueue'
# 銀貨は最大でもMAX_S
s = [s, MAX_S].min
# プライオリティーキュー
q = PriorityQueue.new
# 入力用ラムダ
push = lambda { |v, s, x|
  return if s < 0
  return if dist[v][s] <= x

  dist[v][s] = x
  q.push(Exchange.new(v, s, x))
}

c = []
d = []
n.times do |i|
  cc, dd = input
  c[i]   = cc
  d[i]   = dd
end
push.call(0, s, 0)
until q.empty?
  hoge = q.pop
  v    = hoge.v; s = hoge.s; x = hoge.x
  next if dist[v][s] != x

  ns = [s + c[v], MAX_S].min
  push.call(v, ns, x + d[v])

  g[v].each do |e|
    push.call(e.to, s - e.a, x + e.b)
  end
end


(1 ... n).each do |i|
  ans = INF
  (0 ... (MAX_S + 5)).each do |j|
    ans = [ans, dist[i][j]].min
  end
  puts ans
end

Submission Info

Submission Time
Task E - Two Currencies
User richvote
Language Ruby (2.7.1)
Score 500
Code Size 2665 Byte
Status AC
Exec Time 1273 ms
Memory 21520 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 5
AC × 27
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
All line_1.txt, line_2.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
Case Name Status Exec Time Memory
line_1.txt AC 482 ms 15316 KiB
line_2.txt AC 368 ms 15288 KiB
random_01.txt AC 1176 ms 18728 KiB
random_02.txt AC 1150 ms 21520 KiB
random_03.txt AC 1184 ms 20224 KiB
random_04.txt AC 1273 ms 21052 KiB
random_05.txt AC 1189 ms 20176 KiB
random_06.txt AC 1184 ms 19712 KiB
random_07.txt AC 1159 ms 18356 KiB
random_08.txt AC 1150 ms 19888 KiB
random_09.txt AC 1119 ms 18612 KiB
random_10.txt AC 1269 ms 20796 KiB
random_11.txt AC 1241 ms 21144 KiB
random_12.txt AC 1176 ms 20072 KiB
random_13.txt AC 1165 ms 20096 KiB
random_14.txt AC 1137 ms 18584 KiB
random_15.txt AC 1042 ms 18084 KiB
random_16.txt AC 1232 ms 20568 KiB
random_17.txt AC 1135 ms 21096 KiB
random_18.txt AC 1107 ms 19544 KiB
random_19.txt AC 1087 ms 18324 KiB
random_20.txt AC 1151 ms 18816 KiB
sample_01.txt AC 76 ms 15132 KiB
sample_02.txt AC 93 ms 15300 KiB
sample_03.txt AC 129 ms 15364 KiB
sample_04.txt AC 108 ms 15520 KiB
sample_05.txt AC 67 ms 15300 KiB