Submission #51749960


Source Code Expand

class PQ
	def initialize
		@h = [] # min-heap
	end

	def empty?
		@h.empty?
	end
	def head
		@h[0]
	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 = gets.split.map(&:to_i)
E = Array.new(N+1){[]}
M.times{
	a,b,c = gets.split.map(&:to_i)
	E[a]<<[b,c]
	E[b]<<[a,c]
}

TX = 10**10
T = [nil]*(N+1)
T[1] = 0
que,que2 = PQ.new,PQ.new
que2.enq 0,1
until T[N]&.<TX || que2.empty?
	que,que2 = que2,que
	while (t,a = que.deq)
		next if T[a]<t
		T[a] = t
		E[a].each{|b,c|
			if c<0
				T[b] = TX and que2.enq t+1,b if ! T[b]
			else
				t1 = t<c ? c+1 : t+1
				T[b] = t1 and que.enq t1,b if ! T[b] || t1<T[b]
			end
		}
	end
end

p T[N]

Submission Info

Submission Time
Task H - Winter Road
User ds14050
Language Ruby (ruby 3.2.2)
Score 400
Code Size 1166 Byte
Status AC
Exec Time 674 ms
Memory 61664 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 2
AC × 42
Set Name Test Cases
Sample example0.txt, example1.txt
All example0.txt, example1.txt, random0.txt, random1.txt, random10.txt, random11.txt, random12.txt, random13.txt, random14.txt, random15.txt, random16.txt, random17.txt, random18.txt, random19.txt, random2.txt, random20.txt, random21.txt, random22.txt, random23.txt, random24.txt, random25.txt, random26.txt, random27.txt, random28.txt, random29.txt, random3.txt, random30.txt, random31.txt, random32.txt, random33.txt, random34.txt, random35.txt, random36.txt, random37.txt, random38.txt, random39.txt, random4.txt, random5.txt, random6.txt, random7.txt, random8.txt, random9.txt
Case Name Status Exec Time Memory
example0.txt AC 129 ms 17224 KiB
example1.txt AC 43 ms 17136 KiB
random0.txt AC 350 ms 47504 KiB
random1.txt AC 630 ms 60560 KiB
random10.txt AC 373 ms 48320 KiB
random11.txt AC 514 ms 48956 KiB
random12.txt AC 390 ms 46196 KiB
random13.txt AC 413 ms 47080 KiB
random14.txt AC 527 ms 54772 KiB
random15.txt AC 533 ms 55520 KiB
random16.txt AC 538 ms 53504 KiB
random17.txt AC 345 ms 45432 KiB
random18.txt AC 574 ms 61012 KiB
random19.txt AC 522 ms 61664 KiB
random2.txt AC 315 ms 37596 KiB
random20.txt AC 366 ms 43352 KiB
random21.txt AC 472 ms 56092 KiB
random22.txt AC 377 ms 42848 KiB
random23.txt AC 403 ms 46576 KiB
random24.txt AC 546 ms 51832 KiB
random25.txt AC 157 ms 27884 KiB
random26.txt AC 361 ms 54208 KiB
random27.txt AC 387 ms 47608 KiB
random28.txt AC 428 ms 45368 KiB
random29.txt AC 292 ms 36556 KiB
random3.txt AC 595 ms 54200 KiB
random30.txt AC 146 ms 27268 KiB
random31.txt AC 475 ms 50824 KiB
random32.txt AC 603 ms 55792 KiB
random33.txt AC 384 ms 53508 KiB
random34.txt AC 490 ms 55872 KiB
random35.txt AC 459 ms 50008 KiB
random36.txt AC 562 ms 59876 KiB
random37.txt AC 490 ms 52188 KiB
random38.txt AC 487 ms 55436 KiB
random39.txt AC 216 ms 34792 KiB
random4.txt AC 417 ms 55824 KiB
random5.txt AC 649 ms 57728 KiB
random6.txt AC 424 ms 51568 KiB
random7.txt AC 301 ms 36692 KiB
random8.txt AC 516 ms 48712 KiB
random9.txt AC 674 ms 57396 KiB