Submission #32477294


Source Code Expand

class PQ
	def initialize
		@h = [] # min-heap
	end
 
	def enq c,a
		@h << [c,a]
		up_heap
	end
	def deq
		return unless @h[0]
		@h[0],@h[-1] = @h[-1],@h[0]
		c,a = @h.pop
		dn_heap if @h[0]
		return c,a
	end
 
private
	def up_heap
		c, = up = @h[i=@h.size-1]
		while 0<i
			iP = (i-1)/2
			break if @h[iP][0]<=c
			@h[i],i = @h[iP],iP
		end
		@h[i] = up
	end
	def dn_heap
		c, = dn = @h[i=0]
		z = @h.size
		until z <= iC = i+i+1
			iC += 1 if iC+1<z && @h[iC+1][0]<@h[iC][0]
			break if c<=@h[iC][0]
			@h[i],i = @h[iC],iC
		end
		@h[i] = dn
	end
end

(N,M),*ABCD = $<.map{|ln| ln.split.map(&:to_i) }
E = Array.new(N+1){[]}
ABCD.each{|a,b,c,d|
	next if a==b
	r = Integer.sqrt d
	E[a]<<[b,c,d,r+1]
	E[b]<<[a,c,d,r+1]
}

T = [1.0/0]*(N+1)
T[1] = 0
Q = PQ.new
Q.enq 0,1
while (t,a = Q.deq)
	next if T[a]<t
	E[a].each{|b,c,d,r|
		t1 = (t+2..[r,T[b]-c-d/r].min).bsearch{|t1| d.fdiv(t1)-d.fdiv(t1+1)<1 }||t+1
		t1 += c-1+d/t1
		Q.enq (T[b] = t1),b if t1<T[b]
	}
end

p(T[N].finite?? T[N]:-1)

Submission Info

Submission Time
Task E - Rush Hour 2
User ds14050
Language Ruby (2.7.1)
Score 500
Code Size 1049 Byte
Status AC
Exec Time 560 ms
Memory 68300 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 36
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All hack_01.txt, hack_02.txt, hack_03.txt, hand_01.txt, hand_02.txt, hand_03.txt, max_01.txt, random_01.txt, random_02.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, special_01.txt, special_02.txt, special_03.txt, special_04.txt, special_05.txt, special_06.txt, tester_hack_01.txt
Case Name Status Exec Time Memory
hack_01.txt AC 543 ms 62128 KiB
hack_02.txt AC 545 ms 68300 KiB
hack_03.txt AC 57 ms 14260 KiB
hand_01.txt AC 61 ms 14236 KiB
hand_02.txt AC 53 ms 14168 KiB
hand_03.txt AC 54 ms 14096 KiB
max_01.txt AC 423 ms 66240 KiB
random_01.txt AC 506 ms 61208 KiB
random_02.txt AC 226 ms 44096 KiB
random_11.txt AC 332 ms 52836 KiB
random_12.txt AC 302 ms 48524 KiB
random_13.txt AC 215 ms 37664 KiB
random_14.txt AC 260 ms 44172 KiB
random_15.txt AC 303 ms 50272 KiB
random_16.txt AC 334 ms 52892 KiB
random_17.txt AC 329 ms 52888 KiB
random_18.txt AC 255 ms 41488 KiB
random_21.txt AC 421 ms 66640 KiB
random_22.txt AC 426 ms 66644 KiB
random_23.txt AC 427 ms 66696 KiB
random_24.txt AC 423 ms 66676 KiB
random_31.txt AC 546 ms 62708 KiB
random_32.txt AC 501 ms 62292 KiB
random_33.txt AC 560 ms 62840 KiB
random_34.txt AC 552 ms 62792 KiB
sample_01.txt AC 56 ms 14256 KiB
sample_02.txt AC 55 ms 14032 KiB
sample_03.txt AC 57 ms 14096 KiB
sample_04.txt AC 56 ms 14080 KiB
special_01.txt AC 327 ms 26404 KiB
special_02.txt AC 327 ms 26352 KiB
special_03.txt AC 327 ms 26096 KiB
special_04.txt AC 324 ms 26236 KiB
special_05.txt AC 323 ms 26156 KiB
special_06.txt AC 327 ms 26164 KiB
tester_hack_01.txt AC 410 ms 65272 KiB