提出 #19915433


ソースコード 拡げる

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
  bit.times do |i|
    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
  end
  puts dp[-1].min + 1
end

提出情報

提出日時
問題 E - Magical Ornament
ユーザ konayuki
言語 Ruby (2.7.1)
得点 500
コード長 1047 Byte
結果 AC
実行時間 1930 ms
メモリ 67168 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 60 ms 13980 KiB
02_sample.txt AC 59 ms 14236 KiB
03_sample.txt AC 60 ms 14112 KiB
04_hand.txt AC 61 ms 14160 KiB
05_hand.txt AC 81 ms 19712 KiB
06_small.txt AC 56 ms 14028 KiB
07_small.txt AC 61 ms 14264 KiB
08_small.txt AC 61 ms 14268 KiB
09_small.txt AC 57 ms 14164 KiB
10_small.txt AC 62 ms 14324 KiB
11_small.txt AC 61 ms 14188 KiB
12_small.txt AC 61 ms 14240 KiB
13_small.txt AC 57 ms 14160 KiB
14_small.txt AC 61 ms 13984 KiB
15_small.txt AC 65 ms 14140 KiB
16_small.txt AC 60 ms 14140 KiB
17_small.txt AC 59 ms 14020 KiB
18_small.txt AC 60 ms 14032 KiB
19_small.txt AC 60 ms 14336 KiB
20_small.txt AC 60 ms 14260 KiB
21_large.txt AC 75 ms 18344 KiB
22_large.txt AC 1374 ms 52356 KiB
23_large.txt AC 321 ms 23988 KiB
24_large.txt AC 354 ms 24008 KiB
25_large.txt AC 342 ms 24684 KiB
26_max.txt AC 1845 ms 61736 KiB
27_max.txt AC 1884 ms 61096 KiB
28_max.txt AC 1866 ms 60996 KiB
29_max.txt AC 1842 ms 67168 KiB
30_max.txt AC 1726 ms 64936 KiB
31_max.txt AC 1526 ms 54832 KiB
32_tree.txt AC 1930 ms 63232 KiB
33_tree.txt AC 1879 ms 60592 KiB
34_tree.txt AC 1925 ms 62752 KiB
35_path.txt AC 1811 ms 59240 KiB
36_path.txt AC 1817 ms 59296 KiB
37_path.txt AC 1800 ms 59208 KiB
38_star.txt AC 1742 ms 65172 KiB
39_star.txt AC 1816 ms 65056 KiB
40_star.txt AC 1850 ms 60504 KiB