Submission #13001464


Source Code Expand

class PriorityQueue
  def initialize
    @h = [] # min-heap
  end

  def enq v
    @h << v
    up_heap
  end
  alias :push :enq

  def deq
    min, last = @h[0], @h.pop
    unless @h.empty?
      @h[0] = last
      dn_heap
    end
    return min
  end
  alias :pop :deq

  def empty?
    @h.empty?
  end

  private
  def up_heap
    i, up = @h.size-1, @h[-1]
    while 0 <= (iP = (i-1)/2)
      break unless up < @h[iP]
      @h[i], i = @h[iP], iP
    end
    @h[i] = up
  end

  def dn_heap
    i, dn, size = 0, @h[0], @h.size
    while (iC = 2*i+1) < size
      iC += 1 if iC+1 < size and @h[iC+1] < @h[iC]
      break unless @h[iC] < dn
      @h[i], i = @h[iC], iC
    end
    @h[i] = dn
  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

# プライオリティキューのライブラリを利用
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 2129 Byte
Status AC
Exec Time 1349 ms
Memory 21676 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 491 ms 15444 KiB
line_2.txt AC 379 ms 15224 KiB
random_01.txt AC 1230 ms 18644 KiB
random_02.txt AC 1228 ms 21676 KiB
random_03.txt AC 1232 ms 20416 KiB
random_04.txt AC 1317 ms 19700 KiB
random_05.txt AC 1244 ms 20236 KiB
random_06.txt AC 1232 ms 19896 KiB
random_07.txt AC 1223 ms 18328 KiB
random_08.txt AC 1216 ms 20024 KiB
random_09.txt AC 1152 ms 18484 KiB
random_10.txt AC 1270 ms 20700 KiB
random_11.txt AC 1265 ms 21056 KiB
random_12.txt AC 1257 ms 19916 KiB
random_13.txt AC 1246 ms 19900 KiB
random_14.txt AC 1230 ms 18532 KiB
random_15.txt AC 1102 ms 18256 KiB
random_16.txt AC 1349 ms 20468 KiB
random_17.txt AC 1200 ms 21208 KiB
random_18.txt AC 1165 ms 19656 KiB
random_19.txt AC 1131 ms 18432 KiB
random_20.txt AC 1207 ms 19376 KiB
sample_01.txt AC 78 ms 15288 KiB
sample_02.txt AC 93 ms 15116 KiB
sample_03.txt AC 132 ms 15604 KiB
sample_04.txt AC 113 ms 15476 KiB
sample_05.txt AC 72 ms 15240 KiB