結果

問題 No.1 道のショートカット
ユーザー DialBirdDialBird
提出日時 2019-07-06 22:22:23
言語 Ruby
(3.3.0)
結果
RE  
実行時間 -
コード長 1,101 bytes
コンパイル時間 42 ms
コンパイル使用メモリ 11,364 KB
実行使用メモリ 16,476 KB
最終ジャッジ日時 2023-09-22 13:31:14
合計ジャッジ時間 5,181 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
15,116 KB
testcase_01 AC 78 ms
15,040 KB
testcase_02 AC 78 ms
15,264 KB
testcase_03 AC 80 ms
15,144 KB
testcase_04 AC 78 ms
15,176 KB
testcase_05 AC 82 ms
15,144 KB
testcase_06 AC 78 ms
15,112 KB
testcase_07 AC 80 ms
15,012 KB
testcase_08 AC 82 ms
15,416 KB
testcase_09 AC 81 ms
15,088 KB
testcase_10 AC 81 ms
15,304 KB
testcase_11 AC 81 ms
15,308 KB
testcase_12 AC 81 ms
15,356 KB
testcase_13 AC 83 ms
15,508 KB
testcase_14 AC 79 ms
15,076 KB
testcase_15 RE -
testcase_16 AC 81 ms
15,152 KB
testcase_17 AC 79 ms
15,088 KB
testcase_18 AC 77 ms
15,012 KB
testcase_19 AC 77 ms
15,044 KB
testcase_20 AC 80 ms
15,140 KB
testcase_21 AC 79 ms
15,096 KB
testcase_22 AC 79 ms
15,080 KB
testcase_23 AC 91 ms
16,476 KB
testcase_24 AC 81 ms
15,552 KB
testcase_25 AC 80 ms
15,080 KB
testcase_26 AC 79 ms
15,260 KB
testcase_27 AC 84 ms
15,680 KB
testcase_28 RE -
testcase_29 AC 81 ms
15,504 KB
testcase_30 AC 80 ms
15,088 KB
testcase_31 AC 79 ms
15,412 KB
testcase_32 AC 84 ms
15,352 KB
testcase_33 AC 80 ms
15,084 KB
testcase_34 AC 80 ms
15,300 KB
testcase_35 AC 81 ms
15,344 KB
testcase_36 AC 81 ms
15,404 KB
testcase_37 AC 81 ms
15,516 KB
testcase_38 AC 78 ms
15,228 KB
testcase_39 AC 78 ms
15,172 KB
testcase_40 AC 79 ms
15,272 KB
testcase_41 AC 79 ms
15,236 KB
testcase_42 AC 79 ms
15,120 KB
testcase_43 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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