結果

問題 No.1565 Union
ユーザー 小野寺健小野寺健
提出日時 2021-11-15 10:58:16
言語 Ruby
(3.3.0)
結果
AC  
実行時間 547 ms / 2,000 ms
コード長 433 bytes
コンパイル時間 37 ms
コンパイル使用メモリ 7,552 KB
実行使用メモリ 33,664 KB
最終ジャッジ日時 2024-04-29 18:57:50
合計ジャッジ時間 12,103 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
12,160 KB
testcase_01 AC 87 ms
12,032 KB
testcase_02 AC 95 ms
12,288 KB
testcase_03 AC 79 ms
12,032 KB
testcase_04 AC 78 ms
12,288 KB
testcase_05 AC 79 ms
12,288 KB
testcase_06 AC 82 ms
12,160 KB
testcase_07 AC 81 ms
12,160 KB
testcase_08 AC 82 ms
12,032 KB
testcase_09 AC 79 ms
12,160 KB
testcase_10 AC 273 ms
14,976 KB
testcase_11 AC 368 ms
29,056 KB
testcase_12 AC 375 ms
18,816 KB
testcase_13 AC 173 ms
16,384 KB
testcase_14 AC 416 ms
29,184 KB
testcase_15 AC 502 ms
33,536 KB
testcase_16 AC 477 ms
33,408 KB
testcase_17 AC 522 ms
33,664 KB
testcase_18 AC 528 ms
33,664 KB
testcase_19 AC 547 ms
33,536 KB
testcase_20 AC 489 ms
28,544 KB
testcase_21 AC 496 ms
28,672 KB
testcase_22 AC 488 ms
28,672 KB
testcase_23 AC 499 ms
28,800 KB
testcase_24 AC 465 ms
28,544 KB
testcase_25 AC 498 ms
28,800 KB
testcase_26 AC 430 ms
28,672 KB
testcase_27 AC 478 ms
28,672 KB
testcase_28 AC 508 ms
28,672 KB
testcase_29 AC 517 ms
28,672 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

N, M = gets.split(" ").map{|s| s.to_i}

r = Array.new(N) {Array.new}

M.times {
	a, b = gets.split(" ").map{|s| s.to_i}
	r[a-1] << (b-1)
	r[b-1] << (a-1)
}

d = Array.new(N, Float::INFINITY)

d[0] = 0

q = [0]

fin = false
while not fin and q.length > 0
	v = q.shift
	r[v].each {|nv|
		if d[nv] > d[v] + 1 then
			d[nv] = d[v] + 1
			q << nv
			if nv == N-1 then
				fin = true
				break
			end
		end		
	}
end

puts fin ? d[N-1] : -1
0