Submission #20371092


Source Code Expand

class PQ
	def initialize
		@h = [p] # min-heap, 1-indexed
	end

	def enq t,n
		@h << [t,n]
		up_heap
	end

	def deq
		if 1 < @h.size
			@h[1],@h[-1] = @h[-1],@h[1]
			t,n = @h.pop
			dn_heap
			return t,n
		end
	end

private
	def up_heap
		i = @h.size-1
		while 0 < iP = i/2
			break if @h[iP][0] <= @h[i][0]
			@h[i],@h[iP],i = @h[iP],@h[i],iP
		end
	end

	def dn_heap
		i,sz1 = 1,@h.size-1
		while (iC = i+i) <= sz1
			iC += 1 if iC < sz1 && @h[iC+1][0] < @h[iC][0]
			break if @h[i][0] <= @h[iC][0]
			@h[i],@h[iC],i = @h[iC],@h[i],iC
		end
	end
end

(N,M,X,Y),*ABTK = $<.map{|ln| ln.split.map(&:to_i) }
Tr = ABTK.inject(Array.new(N+1){ [] }){|tr,(a,b,t,k)|
	tr[a] << [b,t,k]
	tr[b] << [a,t,k]
	next tr
}

T = [1.0/0]*(N+1)
Q = PQ.new
Q.enq 0,X
while (t0,n = Q.deq)
	next if T[n] < t0
	T[n] = t0
	break if n == Y

	Tr[n].each{|m,dt,k|
		t = (t0+k-1)/k*k+dt
		next if T[m] <= t
		T[m] = t
		Q.enq t,m
	}
end

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

Submission Info

Submission Time
Task E - Train
User ds14050
Language Ruby (2.7.1)
Score 500
Code Size 998 Byte
Status AC
Exec Time 594 ms
Memory 48260 KiB

Judge Result

Set Name Sample All after_contest
Score / Max Score 0 / 0 500 / 500 0 / 0
Status
AC × 4
AC × 28
AC × 3
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All hand.txt, max_01.txt, random_01.txt, random_02.txt, random_05.txt, random_06.txt, random_07.txt, random_08.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
after_contest after_contest_01.txt, after_contest_02.txt, after_contest_03.txt
Case Name Status Exec Time Memory
after_contest_01.txt AC 63 ms 14300 KiB
after_contest_02.txt AC 560 ms 42764 KiB
after_contest_03.txt AC 594 ms 44864 KiB
hand.txt AC 61 ms 14096 KiB
max_01.txt AC 366 ms 48040 KiB
random_01.txt AC 266 ms 41512 KiB
random_02.txt AC 205 ms 32896 KiB
random_05.txt AC 187 ms 28288 KiB
random_06.txt AC 191 ms 30184 KiB
random_07.txt AC 270 ms 34516 KiB
random_08.txt AC 244 ms 33332 KiB
random_11.txt AC 183 ms 27940 KiB
random_12.txt AC 171 ms 26412 KiB
random_13.txt AC 164 ms 26992 KiB
random_14.txt AC 207 ms 30852 KiB
random_15.txt AC 226 ms 33352 KiB
random_16.txt AC 205 ms 29776 KiB
random_17.txt AC 242 ms 36120 KiB
random_18.txt AC 187 ms 27344 KiB
random_21.txt AC 365 ms 48260 KiB
random_22.txt AC 368 ms 47996 KiB
random_23.txt AC 362 ms 48248 KiB
random_24.txt AC 353 ms 48052 KiB
random_31.txt AC 264 ms 37816 KiB
random_32.txt AC 258 ms 37632 KiB
random_33.txt AC 258 ms 37764 KiB
random_34.txt AC 263 ms 37940 KiB
sample_01.txt AC 64 ms 14164 KiB
sample_02.txt AC 61 ms 13956 KiB
sample_03.txt AC 59 ms 14084 KiB
sample_04.txt AC 59 ms 14224 KiB