結果
問題 |
No.1 道のショートカット
|
ユーザー |
![]() |
提出日時 | 2025-09-04 21:38:21 |
言語 | Crystal (1.14.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,018 bytes |
コンパイル時間 | 12,527 ms |
コンパイル使用メモリ | 311,292 KB |
実行使用メモリ | 7,720 KB |
最終ジャッジ日時 | 2025-09-04 21:38:38 |
合計ジャッジ時間 | 14,232 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 4 |
other | WA * 40 |
ソースコード
n = read_line.to_i c = read_line.to_i v = read_line.to_i # Read all input at once for better performance s_line = read_line.split.map(&.to_i) t_line = read_line.split.map(&.to_i) y_line = read_line.split.map(&.to_i) m_line = read_line.split.map(&.to_i) # Build graph directly without intermediate arrays graph = Array.new(n) { [] of Tuple(Int32, Int32, Int32) } v.times do |i| from = s_line[i] - 1 to = t_line[i] - 1 cost = y_line[i] time = m_line[i] graph[from] << {to, cost, time} end INF = 1_000_000_000 # Use 1D array for DP with rolling optimization dp = Array.new(c + 1, INF) dp[c] = 0 n.times do |i| # Create temporary array to avoid overwriting during iteration temp = dp.dup c.downto(0) do |k| next if temp[k] == INF graph[i].each do |to, cost, time| next if k < cost new_k = k - cost new_time = temp[k] + time if new_time < dp[new_k] dp[new_k] = new_time end end end end result = dp.min puts result == INF ? -1 : result