提出 #19915459


ソースコード 拡げる

n,m = gets.chomp.split(" ").map(&:to_i)
g = Array.new(n+1){Array.new}
m.times do
  a,b = gets.chomp.split(" ").map(&:to_i)
  g[a] << b
  g[b] << a
end
k = gets.to_i
c = gets.chomp.split(" ").map(&:to_i)

G = Array.new(k){Array.new(k,0)}
k.times do |i|
  dist = Array.new(n+1,-1)
  queue = [c[i]]
  dist[c[i]] = 0
  while queue.empty?.!
    v = queue[0]
    queue.shift
    g[v].each do |to|
      if dist[to] == -1
        dist[to] = dist[v] + 1
        queue << to
      end
    end
  end
  k.times do |j|
    next if i == j
    G[i][j] = dist[c[j]]
  end
end

if G.any?{|arr| arr.any?{|v|v == -1}}
  puts -1
else
  bit = 1<<k
  dp = Array.new(bit){Array.new(k,10**9)}
  k.times do |i|
    dp[1<<i][i] = 0
  end
  i = 0
  while i < bit
    zumi,mada = k.times.partition{|v| i[v] == 1}
    zumi.each do |j|
      gj = G[j]
      dpij = dp[i][j]
      mada.each do |k|
        x = dpij + gj[k]
        y = i|(1<<k)
        dp[y][k] = x if dp[y][k] > x
      end
    end
    i += 1
  end
  puts dp[-1].min + 1
end

提出情報

提出日時
問題 E - Magical Ornament
ユーザ konayuki
言語 Ruby (2.7.1)
得点 500
コード長 1065 Byte
結果 AC
実行時間 1742 ms
メモリ 66340 KiB

コンパイルエラー

./Main.rb:33: warning: ambiguous first argument; put parentheses or a space even after `-' operator

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 40
セット名 テストケース
Sample 01_sample.txt, 02_sample.txt, 03_sample.txt
All 01_sample.txt, 02_sample.txt, 03_sample.txt, 04_hand.txt, 05_hand.txt, 06_small.txt, 07_small.txt, 08_small.txt, 09_small.txt, 10_small.txt, 11_small.txt, 12_small.txt, 13_small.txt, 14_small.txt, 15_small.txt, 16_small.txt, 17_small.txt, 18_small.txt, 19_small.txt, 20_small.txt, 21_large.txt, 22_large.txt, 23_large.txt, 24_large.txt, 25_large.txt, 26_max.txt, 27_max.txt, 28_max.txt, 29_max.txt, 30_max.txt, 31_max.txt, 32_tree.txt, 33_tree.txt, 34_tree.txt, 35_path.txt, 36_path.txt, 37_path.txt, 38_star.txt, 39_star.txt, 40_star.txt
ケース名 結果 実行時間 メモリ
01_sample.txt AC 56 ms 14244 KiB
02_sample.txt AC 56 ms 14020 KiB
03_sample.txt AC 57 ms 14032 KiB
04_hand.txt AC 53 ms 14312 KiB
05_hand.txt AC 75 ms 19844 KiB
06_small.txt AC 63 ms 14064 KiB
07_small.txt AC 54 ms 14216 KiB
08_small.txt AC 57 ms 14120 KiB
09_small.txt AC 57 ms 14192 KiB
10_small.txt AC 58 ms 14100 KiB
11_small.txt AC 56 ms 14156 KiB
12_small.txt AC 55 ms 14304 KiB
13_small.txt AC 59 ms 14216 KiB
14_small.txt AC 56 ms 14200 KiB
15_small.txt AC 57 ms 14156 KiB
16_small.txt AC 53 ms 14056 KiB
17_small.txt AC 56 ms 14260 KiB
18_small.txt AC 54 ms 14152 KiB
19_small.txt AC 54 ms 14220 KiB
20_small.txt AC 56 ms 14172 KiB
21_large.txt AC 72 ms 18216 KiB
22_large.txt AC 1288 ms 52916 KiB
23_large.txt AC 309 ms 23960 KiB
24_large.txt AC 328 ms 23896 KiB
25_large.txt AC 329 ms 24500 KiB
26_max.txt AC 1742 ms 62216 KiB
27_max.txt AC 1742 ms 61052 KiB
28_max.txt AC 1731 ms 60608 KiB
29_max.txt AC 1722 ms 66340 KiB
30_max.txt AC 1596 ms 64996 KiB
31_max.txt AC 1437 ms 56328 KiB
32_tree.txt AC 1730 ms 63008 KiB
33_tree.txt AC 1724 ms 60668 KiB
34_tree.txt AC 1723 ms 62576 KiB
35_path.txt AC 1660 ms 59208 KiB
36_path.txt AC 1673 ms 59228 KiB
37_path.txt AC 1710 ms 59372 KiB
38_star.txt AC 1620 ms 65312 KiB
39_star.txt AC 1739 ms 64796 KiB
40_star.txt AC 1701 ms 60344 KiB