# Here your code ! def dot(n,e) <<-EOT graph { node #{ n.times.map{|i| i + 1}.join(' ')}; #{ e.map{|(u,v)| "#{u+1} -- #{v+1};"}.join("\n") } } EOT end INF = 1 << 20 def warshal(n,e) g = n.times.map { Array.new(n,INF) } n.times do |u| g[u][u] = 0 end e.each do |(u,v)| g[u][v] = g[v][u] = 1 end n.times do |i| n.times do |j| n.times do |k| if g[i][k] + g[k][j] < g[i][j] g[j][i] = g[i][j] = g[i][k] + g[k][j] end end end end g end def cover_all?(n,cost) s = 1 << n.size dp = s.times.map { Array.new(n.size,INF) } n.each_with_index do |u,i| dp[1 << i][i] = G[u][0] end s.times do |bit| n.each_with_index do |v,i| to = 1 << i if bit & to > 0 prev = bit ^ to n.each_with_index do |u,j| from = 1 << j if prev & from > 0 uv = dp[prev][j] + G[u][v] if uv < dp[bit][i] dp[bit][i] = uv end end end end end end dp[s - 1].find {|c| c <= cost} end n,m,k = gets.split.map(&:to_i) e = m.times.map { gets.split.map(&:to_i).map{|v| v - 1} } G = warshal(n,e) V = n.times.to_a.drop(1).reverse used = [] V.each do |v| if cover_all?(used.dup << v, k) used << v end end puts used.map{|v| 2 ** v - 1 }.inject(0, &:+)