Submission #30555571


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,K,L),A,B,*UVC = $<.map{|ln| ln.split.map(&:to_i) }
E = Array.new(N){[]}
UVC.each{|u,v,c|
	u -= 1; v -= 1
	E[u]<<[v,c]
	E[v]<<[u,c]
}

D = Array.new(N){[1.0/0,0,1.0/0]}
U = lambda{|d,u,a|
	d1,a1,d2 = D[u]
	next if d2<=d || a1==a&&d1<=d
	if a1==a
		D[u][0] = d
	elsif d<d1
		D[u] = d,a,d1
	else
		D[u][2] = d
	end
}
Q = PQ.new
B.each{|u|
	a = A[u-=1]
	U[0,u,a]
	Q.enq 0,[u,a]
}
while (d,(u,a) = Q.deq)
	d1,a1,d2 = D[u]
	E[u].each{|v,c|
		Q.enq d+c,[v,a] if U[d+c,v,a]
	} if d<=d1 || d<=d2&&a!=a1
end

puts A.map.with_index{|a,i|
	d1,a1,d2 = D[i]
	d = a!=a1 ? d1 : d2
	next d.finite? ? d : -1
}*' '

Submission Info

Submission Time
Task G - Foreign Friends
User ds14050
Language Ruby (2.7.1)
Score 600
Code Size 1230 Byte
Status AC
Exec Time 1087 ms
Memory 70424 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 58
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, hand_16.txt, hand_17.txt, hand_18.txt, hand_19.txt, hand_20.txt, hand_21.txt, hand_22.txt, hand_23.txt, killer_00.txt, killer_01.txt, killer_02.txt, killer_03.txt, killer_b_00.txt, killer_b_01.txt, killer_b_02.txt, killer_b_03.txt, random_00.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, random_21.txt, random_22.txt, random_23.txt
Case Name Status Exec Time Memory
example_00.txt AC 57 ms 14052 KiB
example_01.txt AC 58 ms 14128 KiB
hand_00.txt AC 1064 ms 63768 KiB
hand_01.txt AC 929 ms 63696 KiB
hand_02.txt AC 1081 ms 65824 KiB
hand_03.txt AC 936 ms 64016 KiB
hand_04.txt AC 982 ms 63512 KiB
hand_05.txt AC 936 ms 64248 KiB
hand_06.txt AC 982 ms 62832 KiB
hand_07.txt AC 791 ms 68148 KiB
hand_08.txt AC 950 ms 62852 KiB
hand_09.txt AC 773 ms 63416 KiB
hand_10.txt AC 763 ms 63492 KiB
hand_11.txt AC 722 ms 64568 KiB
hand_12.txt AC 750 ms 64464 KiB
hand_13.txt AC 750 ms 64468 KiB
hand_14.txt AC 739 ms 63088 KiB
hand_15.txt AC 585 ms 67476 KiB
hand_16.txt AC 718 ms 62808 KiB
hand_17.txt AC 899 ms 63180 KiB
hand_18.txt AC 493 ms 67616 KiB
hand_19.txt AC 495 ms 67604 KiB
hand_20.txt AC 427 ms 55364 KiB
hand_21.txt AC 966 ms 69984 KiB
hand_22.txt AC 968 ms 70328 KiB
hand_23.txt AC 994 ms 70424 KiB
killer_00.txt AC 458 ms 46244 KiB
killer_01.txt AC 857 ms 63480 KiB
killer_02.txt AC 582 ms 46476 KiB
killer_03.txt AC 670 ms 65984 KiB
killer_b_00.txt AC 452 ms 67640 KiB
killer_b_01.txt AC 531 ms 63396 KiB
killer_b_02.txt AC 548 ms 63396 KiB
killer_b_03.txt AC 537 ms 64588 KiB
random_00.txt AC 296 ms 53052 KiB
random_01.txt AC 208 ms 24332 KiB
random_02.txt AC 95 ms 16960 KiB
random_03.txt AC 1025 ms 64928 KiB
random_04.txt AC 602 ms 44480 KiB
random_05.txt AC 91 ms 16744 KiB
random_06.txt AC 1000 ms 69400 KiB
random_07.txt AC 207 ms 24064 KiB
random_08.txt AC 357 ms 35704 KiB
random_09.txt AC 1087 ms 65120 KiB
random_10.txt AC 208 ms 24240 KiB
random_11.txt AC 96 ms 16984 KiB
random_12.txt AC 304 ms 53084 KiB
random_13.txt AC 458 ms 41988 KiB
random_14.txt AC 321 ms 33304 KiB
random_15.txt AC 970 ms 63644 KiB
random_16.txt AC 602 ms 44216 KiB
random_17.txt AC 95 ms 17000 KiB
random_18.txt AC 1041 ms 65048 KiB
random_19.txt AC 462 ms 45128 KiB
random_20.txt AC 307 ms 33292 KiB
random_21.txt AC 994 ms 69396 KiB
random_22.txt AC 208 ms 24768 KiB
random_23.txt AC 309 ms 33232 KiB