# Here your code ! INF = 1 << 10 def warshal(n,e) g = n.times.map {|i| n.times.map {|j| i == j ? 0 : INF} } e.each do |u,v| g[u][v] = g[v][u] = 1 end n.times do |i| n.times do |j| g[j][i] = g[i][j] = n.times.map {|k| g[i][k] + g[k][j] }.min end end g end def cover_all?(n, cost) s = 1 << (n.size - 1) t = (1 << n.size) - 1 (s .. t).each do |bit| n.each_with_index do |u,i| mask = 1 << i if mask & bit > 0 DP[bit][i] = DP[mask ^ bit].each_with_index.map {|used, j| if ((mask ^ bit) & (1 << j)) > 0 v = n[j] used + G[u][v] else INF end }.min end end end DP[t].min <= cost end n,m,k = gets.split.map(&:to_i) e = m.times.map do gets.split.map(&:to_i).map{|v| v - 1} end G = warshal(n, e) V = n.times.to_a.drop(1).reverse visited = [0] DP = (1 << (k+1)).times.map { Array.new(k + 1, INF) } DP[1][0] = 0 V.all? do |v| if cover_all?(visited.dup << v, k + 1) visited << v end visited.size < k + 1 end puts visited.inject(0){|s,v| s - 1 + (1 << v) }