結果

問題 No.1 道のショートカット
ユーザー DialBirdDialBird
提出日時 2019-07-06 22:27:50
言語 Ruby
(3.3.0)
結果
AC  
実行時間 87 ms / 5,000 ms
コード長 1,136 bytes
コンパイル時間 61 ms
コンパイル使用メモリ 11,552 KB
実行使用メモリ 16,488 KB
最終ジャッジ日時 2023-09-27 22:20:22
合計ジャッジ時間 5,425 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
15,108 KB
testcase_01 AC 76 ms
15,128 KB
testcase_02 AC 74 ms
15,176 KB
testcase_03 AC 72 ms
15,116 KB
testcase_04 AC 73 ms
15,208 KB
testcase_05 AC 75 ms
15,272 KB
testcase_06 AC 73 ms
15,124 KB
testcase_07 AC 76 ms
15,276 KB
testcase_08 AC 78 ms
15,436 KB
testcase_09 AC 76 ms
15,308 KB
testcase_10 AC 75 ms
15,296 KB
testcase_11 AC 73 ms
15,324 KB
testcase_12 AC 75 ms
15,392 KB
testcase_13 AC 76 ms
15,288 KB
testcase_14 AC 72 ms
15,288 KB
testcase_15 AC 75 ms
15,156 KB
testcase_16 AC 74 ms
15,380 KB
testcase_17 AC 74 ms
15,076 KB
testcase_18 AC 76 ms
15,268 KB
testcase_19 AC 73 ms
15,124 KB
testcase_20 AC 73 ms
15,276 KB
testcase_21 AC 73 ms
15,228 KB
testcase_22 AC 75 ms
15,128 KB
testcase_23 AC 87 ms
16,488 KB
testcase_24 AC 77 ms
15,392 KB
testcase_25 AC 74 ms
15,132 KB
testcase_26 AC 75 ms
15,072 KB
testcase_27 AC 78 ms
15,636 KB
testcase_28 AC 74 ms
15,140 KB
testcase_29 AC 77 ms
15,460 KB
testcase_30 AC 74 ms
15,136 KB
testcase_31 AC 74 ms
15,280 KB
testcase_32 AC 75 ms
15,536 KB
testcase_33 AC 76 ms
15,168 KB
testcase_34 AC 76 ms
15,508 KB
testcase_35 AC 77 ms
15,468 KB
testcase_36 AC 74 ms
15,572 KB
testcase_37 AC 75 ms
15,396 KB
testcase_38 AC 70 ms
15,176 KB
testcase_39 AC 74 ms
15,240 KB
testcase_40 AC 77 ms
15,116 KB
testcase_41 AC 77 ms
15,216 KB
testcase_42 AC 76 ms
15,136 KB
testcase_43 AC 73 ms
15,040 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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]].nil? || 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