結果

問題 No.1473 おでぶなおばけさん
ユーザー 小野寺健小野寺健
提出日時 2021-10-31 11:10:27
言語 Ruby
(3.3.0)
結果
AC  
実行時間 457 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 127 ms
コンパイル使用メモリ 7,424 KB
実行使用メモリ 28,032 KB
最終ジャッジ日時 2024-04-17 03:41:39
合計ジャッジ時間 16,582 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
12,288 KB
testcase_01 AC 74 ms
12,160 KB
testcase_02 AC 405 ms
27,264 KB
testcase_03 AC 337 ms
23,552 KB
testcase_04 AC 249 ms
19,840 KB
testcase_05 AC 135 ms
14,592 KB
testcase_06 AC 348 ms
24,576 KB
testcase_07 AC 425 ms
27,904 KB
testcase_08 AC 457 ms
27,904 KB
testcase_09 AC 425 ms
27,776 KB
testcase_10 AC 290 ms
20,992 KB
testcase_11 AC 285 ms
20,736 KB
testcase_12 AC 288 ms
20,480 KB
testcase_13 AC 219 ms
17,920 KB
testcase_14 AC 178 ms
16,000 KB
testcase_15 AC 263 ms
20,352 KB
testcase_16 AC 274 ms
20,736 KB
testcase_17 AC 95 ms
12,800 KB
testcase_18 AC 100 ms
13,056 KB
testcase_19 AC 245 ms
18,560 KB
testcase_20 AC 297 ms
22,656 KB
testcase_21 AC 322 ms
21,632 KB
testcase_22 AC 378 ms
26,368 KB
testcase_23 AC 335 ms
23,936 KB
testcase_24 AC 347 ms
24,192 KB
testcase_25 AC 368 ms
26,368 KB
testcase_26 AC 376 ms
25,984 KB
testcase_27 AC 204 ms
17,280 KB
testcase_28 AC 388 ms
26,624 KB
testcase_29 AC 279 ms
21,120 KB
testcase_30 AC 326 ms
21,888 KB
testcase_31 AC 444 ms
28,032 KB
testcase_32 AC 349 ms
24,832 KB
testcase_33 AC 308 ms
22,912 KB
testcase_34 AC 196 ms
17,152 KB
testcase_35 AC 205 ms
17,280 KB
testcase_36 AC 334 ms
23,424 KB
testcase_37 AC 304 ms
22,272 KB
testcase_38 AC 126 ms
14,336 KB
testcase_39 AC 273 ms
20,736 KB
testcase_40 AC 276 ms
20,736 KB
testcase_41 AC 277 ms
20,608 KB
testcase_42 AC 274 ms
20,736 KB
testcase_43 AC 331 ms
21,760 KB
testcase_44 AC 334 ms
21,760 KB
testcase_45 AC 339 ms
21,632 KB
testcase_46 AC 370 ms
23,680 KB
testcase_47 AC 406 ms
26,240 KB
testcase_48 AC 396 ms
24,704 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

n, m = gets.split(" ").map{|s| s.to_i}

r = []

m.times {
	r << gets.split(" ").map{|s| s.to_i}
}

class UnionFind
	def initialize(n)
		@par = 0.upto(n-1).to_a
		@rank = Array.new(n, 0)
	end
	
	def unite(x, y)
		x = find(x)
		y = find(y)
		return if x == y
		if @rank[x] < @rank[y] then
			@par[x] = y
		else
			@par[y] = x
			@rank[x] += 1 if @rank[x] == @rank[y]
		end
	end
	
	def same?(x, y)
		find(x) == find(y)
	end
	
	private
	
	def find(x)
		if @par[x] == x then
			x
		else
			@par[x] = find(@par[x])
		end
	end
end

r.sort_by!{|x| -x[2]}

v = Array.new(n) {Array.new}

uf = UnionFind.new(n)

maxd = 0
same = false

while r.length > 0 do
	s, t, d = r.shift
	break if same and d < maxd
	maxd = d
	uf.unite(s-1, t-1)
	v[s-1] << t-1
	v[t-1] << s-1
	same = uf.same?(0, n-1)
end

dist = Array.new(n, Float::INFINITY)
dist[0] = 0

q = [[0, 0]]
cost = -1
while q.length > 0 and cost < 0 do
	cv, c = q.shift
	v[cv].each {|nv|
		if nv == n-1 then
			cost = c + 1
			break
		end
		if dist[nv] > c + 1
			dist[nv] = c + 1
			q << [nv, c+1]
		end
	}
end

puts "#{maxd} #{cost}"
0