結果

問題 No.1 道のショートカット
ユーザー shi-moshi-mo
提出日時 2016-03-22 02:35:28
言語 Ruby
(3.3.0)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 1,244 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 11,520 KB
実行使用メモリ 16,128 KB
最終ジャッジ日時 2023-09-27 21:53:34
合計ジャッジ時間 4,738 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
15,212 KB
testcase_01 AC 69 ms
15,248 KB
testcase_02 AC 73 ms
15,300 KB
testcase_03 AC 69 ms
15,344 KB
testcase_04 AC 68 ms
15,332 KB
testcase_05 AC 69 ms
15,176 KB
testcase_06 AC 68 ms
15,340 KB
testcase_07 AC 68 ms
15,328 KB
testcase_08 AC 70 ms
15,480 KB
testcase_09 AC 69 ms
15,288 KB
testcase_10 AC 69 ms
15,428 KB
testcase_11 AC 72 ms
15,556 KB
testcase_12 AC 71 ms
15,584 KB
testcase_13 AC 71 ms
15,520 KB
testcase_14 AC 68 ms
15,304 KB
testcase_15 AC 68 ms
15,172 KB
testcase_16 AC 71 ms
15,120 KB
testcase_17 AC 69 ms
15,292 KB
testcase_18 AC 68 ms
15,040 KB
testcase_19 AC 68 ms
15,328 KB
testcase_20 AC 68 ms
15,144 KB
testcase_21 AC 68 ms
15,336 KB
testcase_22 AC 68 ms
15,136 KB
testcase_23 AC 74 ms
15,940 KB
testcase_24 AC 70 ms
15,568 KB
testcase_25 AC 69 ms
15,136 KB
testcase_26 AC 68 ms
15,152 KB
testcase_27 AC 75 ms
16,128 KB
testcase_28 AC 68 ms
15,140 KB
testcase_29 AC 72 ms
15,588 KB
testcase_30 AC 71 ms
15,204 KB
testcase_31 AC 70 ms
15,408 KB
testcase_32 AC 70 ms
15,404 KB
testcase_33 AC 71 ms
15,376 KB
testcase_34 AC 72 ms
15,616 KB
testcase_35 AC 71 ms
15,400 KB
testcase_36 AC 72 ms
15,412 KB
testcase_37 AC 71 ms
15,296 KB
testcase_38 AC 68 ms
15,140 KB
testcase_39 AC 70 ms
15,228 KB
testcase_40 AC 70 ms
15,136 KB
testcase_41 AC 68 ms
15,356 KB
testcase_42 AC 69 ms
15,040 KB
testcase_43 AC 67 ms
15,136 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.rb:57: warning: ambiguous first argument; put parentheses or a space even after `-' operator
Main.rb:31: warning: assigned but unused variable - num_ways
Main.rb:36: warning: assigned but unused variable - pos_head
Syntax OK

ソースコード

diff #

#!ruby

def load_int(io)
  io.gets.to_i
end

def load_int_array(io)
  io.gets.chomp.split.map(&:to_i)
end

def load_problem(io)
  num_cities = load_int(io)
  cost_limit = load_int(io)
  num_ways   = load_int(io)

  s = load_int_array(io)
  t = load_int_array(io)
  y = load_int_array(io)
  m = load_int_array(io)

  ways = []
  num_ways.times do |i|
    ways << { start: s[i], terminal: t[i], cost: y[i], time: m[i] }
  end
  ways_from = ways.group_by{|way| way[:start] }

  [ num_cities, cost_limit, num_ways, ways_from ]
end

def main
  num_cities, cost_limit, num_ways, ways_from = load_problem(STDIN)

  candidates = [ { pos: 1, cost: 0, time: 0 } ]
  result = nil

pos_head = 1
  until candidates.empty?
    candidates.sort_by!{|c| c[:time] }

    current = candidates.shift
    if num_cities == current[:pos]
      result = current
      break
    end

    next unless ways_from[current[:pos]]

    ways_from[current[:pos]].each do |next_way|
      cost = current[:cost] + next_way[:cost]
      time = current[:time] + next_way[:time]
      next if cost_limit < cost
      candidates.unshift({ pos: next_way[:terminal], cost: cost, time: time })
    end
  end

  if result.nil?
    puts -1
    exit 0
  end
  puts result[:time]
end

main
0