結果

問題 No.3263 違法な散歩道
ユーザー Nanashi.
提出日時 2025-09-06 15:02:54
言語 Ruby
(3.4.1)
結果
AC  
実行時間 1,877 ms / 2,000 ms
コード長 868 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 8,192 KB
実行使用メモリ 145,200 KB
最終ジャッジ日時 2025-09-06 15:03:34
合計ジャッジ時間 36,357 ms
ジャッジサーバーID
(参考情報)
judge3 / judge
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

# frozen_string_literal: true
in_n, in_m = gets.chomp.split.map(&:to_i)
in_uv = in_m.times.map { gets.chomp.split.map(&:to_i) }
in_k = gets.chomp.to_i
in_a =
  if in_k > 0
    gets.chomp.split.map(&:to_i).to_set
  else
    Set[]
  end

edges = Hash.new { |h, k| h[k] = Set.new }

in_uv.each do |u, v|
  5.times do |i|
    shift = in_n * i
    if in_a.include?(v)
      edges[u + shift] << v + shift + in_n if i < 4
    else
      edges[u + shift] << v
    end

    if in_a.include?(u)
      edges[v + shift] << u + shift + in_n if i < 4
    else
      edges[v + shift] << u
    end
  end
end

queue = [1]
distances = {}
distances[1] = 0
until queue.empty?
  q = queue.shift
  edges[q].each do |e|
    if distances[e].nil?
      distances[e] = distances[q] + 1
      queue << e
    end
  end
end

puts (5.times.map { |i| distances[in_n + in_n * i] }.compact.min || -1)
0