Submission #12489199


Source Code Expand

# Studying https://img.atcoder.jp/abc164/editorial.pdf

# Inputs

N,M,S = gets.split.map(&:to_i)
E,amax = Hash.new{|h,k| h[k]={} }, 0
M.times.map{
	u,v,a,b = gets.split.map(&:to_i)
	E[u-1][v-1] = E[v-1][u-1] = [a,b]
	amax = a if amax < a
}
X = N.times.map{ gets.split.map(&:to_i) }

# Tools

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

	def enq(v)
		@h.push(v)
		up_heap
	end

	def deq
		max, last = @h[0], @h.pop
		unless @h.empty?
			@h[0] = last
			dn_heap
		end
		return max
	end

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

	def dn_heap
		i, dn, size = 0, @h[0], @h.size
		while (iC = 2*i+1) < size
			iC += 1 if iC+1 < size and @h[iC+1] < @h[iC]
			break unless @h[iC] < dn
			@h[i], i = @h[iC], iC
		end
		@h[i] = dn
	end
end

R = lambda{|t,s,u|
	next t<<32 | [s,(N-1)*amax].min<<16 | u
}
Я = lambda{|r|
	next r>>32, r>>16&4095, r&63
}

# Main

n,T,2 = 0, [nil]*N, [-1]*N
Q = PQ.new; Q.enq R[0,S,0]
while k = Q.deq
	t,s,u = Я[k]

	if T[u].nil? # first arrived.
		n, T[u] = n+1, t
		break if n == N # completed.
	end

	next if s < 2[u] # 銭なしの再訪に用なし
	2[u] = s

	c,d = X[u]
	Q.enq R[t+d,s+c,u]

	E[u].each{|v,(a,b)|
		Q.enq R[t+b,s-a,v] if a<=s
	}
end

# Output

T.shift
p *T

Submission Info

Submission Time
Task E - Two Currencies
User ds14050
Language Ruby (2.7.1)
Score 0
Code Size 1416 Byte
Status TLE
Exec Time 2206 ms
Memory 19760 KiB

Compile Error

./Main.rb:89: warning: `*' interpreted as argument prefix

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 5
AC × 26
TLE × 1
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
All line_1.txt, line_2.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.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_19.txt, random_20.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt
Case Name Status Exec Time Memory
line_1.txt AC 56 ms 14372 KiB
line_2.txt TLE 2206 ms 19760 KiB
random_01.txt AC 68 ms 14768 KiB
random_02.txt AC 489 ms 16900 KiB
random_03.txt AC 64 ms 14424 KiB
random_04.txt AC 70 ms 14752 KiB
random_05.txt AC 92 ms 15180 KiB
random_06.txt AC 64 ms 14456 KiB
random_07.txt AC 56 ms 14384 KiB
random_08.txt AC 66 ms 14604 KiB
random_09.txt AC 59 ms 14292 KiB
random_10.txt AC 120 ms 15716 KiB
random_11.txt AC 64 ms 14668 KiB
random_12.txt AC 61 ms 14432 KiB
random_13.txt AC 77 ms 14684 KiB
random_14.txt AC 59 ms 14448 KiB
random_15.txt AC 63 ms 14772 KiB
random_16.txt AC 69 ms 14720 KiB
random_17.txt AC 276 ms 18268 KiB
random_18.txt AC 58 ms 14440 KiB
random_19.txt AC 58 ms 14232 KiB
random_20.txt AC 60 ms 14228 KiB
sample_01.txt AC 54 ms 14292 KiB
sample_02.txt AC 53 ms 14228 KiB
sample_03.txt AC 52 ms 14388 KiB
sample_04.txt AC 51 ms 14360 KiB
sample_05.txt AC 53 ms 14384 KiB