結果

問題 No.1 道のショートカット
ユーザー DialBird
提出日時 2019-07-06 22:22:23
言語 Ruby
(3.4.1)
結果
RE  
実行時間 -
コード長 1,101 bytes
コンパイル時間 68 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-07-08 05:12:08
合計ジャッジ時間 4,522 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37 RE * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

def main
  n = gets.to_i
  c = gets.to_i
  v = gets.to_i
  s = gets.split.map(&:to_i)
  t = gets.split.map(&:to_i)
  y = gets.split.map(&:to_i)
  m = gets.split.map(&:to_i)

  ways_from = []
  v.times do |i|
    if ways_from[s[i]]
      ways_from[s[i]] << { next: t[i], cost: y[i], min: m[i] }
    else
      ways_from[s[i]] = [{ next: t[i], cost: y[i], min: m[i] }]
    end
  end
  candidates = [{ pos: 1, cost: 0, min: 0 }]
  res = nil

  while 0 < candidates.size
    candidates.sort_by! { |candidate| candidate[:min] }
    candidate = candidates.shift

    # もし現在地がゴールだったら終了
    if candidate[:pos] == n
      res = candidate[:min]
      break
    end

    # もし移動先がなかったら終了
    next if ways_from[candidate[:pos]].empty?

    # もしお金が足りなかったら終了
    ways_from[candidate[:pos]].each do |next_way|
      next_cost = candidate[:cost] + next_way[:cost]
      next if c < next_cost

      candidates << { pos: next_way[:next], cost: next_cost, min: candidate[:min] + next_way[:min]}
    end
  end

  res || -1
end

puts main
0