Submission #14141040


Source Code Expand

class PQ
	K,V = *0..1

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

	def enq k,v
		@h.push [k,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[K] < @h[iP][K]
			@h[i], i = @h[iP], iP
		end
		@h[i] = up
	end

	def dn_heap
		i, dn, sz = 0, @h[0], @h.size
		while (iC = 2*i+1) < sz
			iC += 1 if iC+1 < sz and @h[iC+1][K] < @h[iC][K]
			break unless @h[iC][K] < dn[K]
			@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{
	u,v = gets.split.map(&:to_i)
	E[u]<<v
	E[v]<<u
}
S = gets.to_i
K = gets.to_i
T = gets.split.map(&:to_i)

C = lambda{|s,t|
	cost,q,visited = 0,[s],{s=>true}
	until q.include? t
		q = q.map{|u|E[u]}.inject(:|).select{|_| ! visited[_] && (visited[_]=true) }
		cost+=1
	end
	next cost
}
E2 = Hash.new{|h,u|h[u]=[]}
V = [S,*T]
V.combination(2){|s,t|
	c = C[s,t]
	E2[s]<<[t,c]
	E2[t]<<[s,c]
}
Visit = Hash[*V.map.with_index{|v,i| [v,1<<i] }.flatten]
Visited = lambda{
	h = {}
	return lambda{|u,visited|
		k = u<<K+1 | visited | Visit[u]
		return h[k] || ! (h[k]=true)
	}
}.call
Q = PQ.new
Q.enq 0,[S,1]
while (c,(u,visited) = Q.deq)
	if visited == (1<<K+1)-1
		p c
		break
	end
	E2[u].each{|v,cv|
		next if Visited[v,visited]
		Q.enq c+cv,[v,visited|Visit[v]]
	}
end

Submission Info

Submission Time
Task M - Real Traveling Salesman
User ds14050
Language Ruby (2.7.1)
Score 0
Code Size 1505 Byte
Status TLE
Exec Time 2760 ms
Memory 133860 KiB

Judge Result

Set Name All Sample
Score / Max Score 0 / 6 0 / 0
Status
AC × 17
TLE × 20
AC × 2
Set Name Test Cases
All sample_01.txt, sample_02.txt, testcase_1.txt, testcase_10.txt, testcase_11.txt, testcase_12.txt, testcase_13.txt, testcase_14.txt, testcase_15.txt, testcase_16.txt, testcase_17.txt, testcase_18.txt, testcase_19.txt, testcase_2.txt, testcase_20.txt, testcase_21.txt, testcase_22.txt, testcase_23.txt, testcase_24.txt, testcase_25.txt, testcase_26.txt, testcase_27.txt, testcase_28.txt, testcase_29.txt, testcase_3.txt, testcase_30.txt, testcase_31.txt, testcase_32.txt, testcase_33.txt, testcase_34.txt, testcase_35.txt, testcase_4.txt, testcase_5.txt, testcase_6.txt, testcase_7.txt, testcase_8.txt, testcase_9.txt
Sample sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
sample_01.txt AC 52 ms 14076 KiB
sample_02.txt AC 53 ms 14172 KiB
testcase_1.txt TLE 2613 ms 114876 KiB
testcase_10.txt AC 341 ms 26436 KiB
testcase_11.txt AC 352 ms 26492 KiB
testcase_12.txt AC 73 ms 17688 KiB
testcase_13.txt TLE 2642 ms 47384 KiB
testcase_14.txt AC 1297 ms 26288 KiB
testcase_15.txt AC 139 ms 19412 KiB
testcase_16.txt AC 2451 ms 46176 KiB
testcase_17.txt AC 104 ms 16452 KiB
testcase_18.txt AC 133 ms 17760 KiB
testcase_19.txt TLE 2759 ms 114344 KiB
testcase_2.txt TLE 2758 ms 61576 KiB
testcase_20.txt TLE 2759 ms 108924 KiB
testcase_21.txt TLE 2759 ms 115296 KiB
testcase_22.txt TLE 2759 ms 126132 KiB
testcase_23.txt TLE 2759 ms 108544 KiB
testcase_24.txt TLE 2760 ms 121420 KiB
testcase_25.txt TLE 2760 ms 133860 KiB
testcase_26.txt TLE 2759 ms 115908 KiB
testcase_27.txt TLE 2760 ms 115400 KiB
testcase_28.txt TLE 2760 ms 119816 KiB
testcase_29.txt TLE 2760 ms 113924 KiB
testcase_3.txt TLE 2758 ms 54652 KiB
testcase_30.txt TLE 2759 ms 118032 KiB
testcase_31.txt TLE 2760 ms 122004 KiB
testcase_32.txt TLE 2759 ms 122756 KiB
testcase_33.txt TLE 2760 ms 124664 KiB
testcase_34.txt AC 52 ms 14132 KiB
testcase_35.txt AC 57 ms 14224 KiB
testcase_4.txt TLE 2614 ms 47024 KiB
testcase_5.txt AC 312 ms 26400 KiB
testcase_6.txt AC 319 ms 26412 KiB
testcase_7.txt AC 150 ms 21712 KiB
testcase_8.txt AC 285 ms 23516 KiB
testcase_9.txt AC 692 ms 26148 KiB