結果

問題 No.1577 織姫と彦星2
ユーザー yuruhiyayuruhiya
提出日時 2021-06-29 20:28:43
言語 Crystal
(1.11.2)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 5,767 bytes
コンパイル時間 21,386 ms
コンパイル使用メモリ 257,156 KB
実行使用メモリ 11,384 KB
最終ジャッジ日時 2023-09-08 05:40:46
合計ジャッジ時間 25,993 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,360 KB
testcase_02 AC 2 ms
4,400 KB
testcase_03 AC 2 ms
4,360 KB
testcase_04 AC 3 ms
4,392 KB
testcase_05 AC 16 ms
6,084 KB
testcase_06 AC 6 ms
5,076 KB
testcase_07 AC 51 ms
9,328 KB
testcase_08 AC 19 ms
6,508 KB
testcase_09 AC 82 ms
10,956 KB
testcase_10 AC 18 ms
6,364 KB
testcase_11 AC 5 ms
4,880 KB
testcase_12 AC 34 ms
7,172 KB
testcase_13 AC 18 ms
6,300 KB
testcase_14 AC 45 ms
8,196 KB
testcase_15 AC 16 ms
5,776 KB
testcase_16 AC 47 ms
9,044 KB
testcase_17 AC 27 ms
7,060 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 49 ms
8,660 KB
testcase_20 AC 41 ms
8,180 KB
testcase_21 AC 48 ms
9,052 KB
testcase_22 AC 46 ms
9,016 KB
testcase_23 AC 22 ms
6,696 KB
testcase_24 AC 62 ms
9,764 KB
testcase_25 AC 14 ms
5,584 KB
testcase_26 AC 23 ms
6,760 KB
testcase_27 AC 42 ms
8,244 KB
testcase_28 AC 42 ms
8,344 KB
testcase_29 AC 24 ms
6,496 KB
testcase_30 AC 29 ms
6,952 KB
testcase_31 AC 50 ms
9,092 KB
testcase_32 AC 3 ms
4,696 KB
testcase_33 AC 3 ms
4,460 KB
testcase_34 AC 23 ms
6,764 KB
testcase_35 AC 44 ms
8,904 KB
testcase_36 AC 46 ms
9,028 KB
testcase_37 AC 71 ms
10,572 KB
testcase_38 AC 68 ms
10,492 KB
testcase_39 AC 20 ms
6,620 KB
testcase_40 AC 20 ms
6,708 KB
testcase_41 AC 51 ms
9,236 KB
testcase_42 AC 36 ms
7,960 KB
testcase_43 AC 37 ms
7,900 KB
testcase_44 AC 4 ms
4,704 KB
testcase_45 AC 12 ms
5,352 KB
testcase_46 AC 13 ms
5,584 KB
testcase_47 AC 48 ms
8,348 KB
testcase_48 AC 19 ms
6,384 KB
testcase_49 AC 26 ms
6,920 KB
testcase_50 AC 20 ms
6,548 KB
testcase_51 AC 7 ms
5,040 KB
testcase_52 AC 67 ms
9,932 KB
testcase_53 AC 67 ms
11,384 KB
testcase_54 AC 28 ms
6,868 KB
testcase_55 AC 63 ms
9,440 KB
testcase_56 AC 17 ms
5,888 KB
testcase_57 AC 10 ms
5,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# require "/graph/bfs"
# require "./graph"
struct Edge(T)
  include Comparable(Edge(T))

  property to : Int32
  property cost : T

  def initialize(@to : Int32, @cost : T)
  end

  def <=>(other : Edge(T))
    {cost, to} <=> {other.cost, other.to}
  end

  def to_s(io) : Nil
    io << '(' << to << ", " << cost << ')'
  end

  def inspect(io) : Nil
    io << "->#{to}(#{cost})"
  end
end

struct Edge2(T)
  include Comparable(Edge2(T))

  property from : Int32
  property to : Int32
  property cost : T

  def initialize(@from : Int32, @to : Int32, @cost : T)
  end

  def <=>(other : Edge2(T))
    {cost, from, to} <=> {other.cost, other.from, other.to}
  end

  def reverse
    Edge2(T).new(to, from, cost)
  end

  def to_s(io) : Nil
    io << '(' << from << ", " << to << ", " << cost << ')'
  end

  def inspect(io) : Nil
    io << "#{from}->#{to}(#{cost})"
  end
end

struct UnweightedEdge2
  property from : Int32
  property to : Int32

  def initialize(@from, @to)
  end

  def reverse
    UnweightedEdge2.new(to, from)
  end

  def to_s(io) : Nil
    io << '(' << from << ", " << to << ')'
  end

  def inspect(io) : Nil
    io << "#{from}->#{to}"
  end
end

abstract class Graph(T)
  getter graph : Array(Array(Edge(T)))

  def initialize(size : Int)
    raise ArgumentError.new("Negative graph size: #{size}") unless size >= 0
    @graph = Array.new(size) { Array(Edge(T)).new }
  end

  def add_edge(i : Int32, j : Int32, cost : T)
    add_edge(Edge2.new(i, j, cost))
  end

  def add_edges(edges : Array(Edge2(T)))
    edges.each { |edge| add_edge(edge) }
    self
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  def each_edge : Nil
    (0...size).each do |v|
      graph[v].each do |edge|
        yield Edge2(T).new(v, edge.to, edge.cost)
      end
    end
  end

  def edges
    result = [] of Edge2(T)
    each_edge do |edge|
      result << edge
    end
    result
  end
end

class DirectedGraph(T) < Graph(T)
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges : Array(Edge2(T)))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : Edge2(T))
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 0 <= edge.to < size
    @graph[edge.from] << Edge.new(edge.to, edge.cost)
    self
  end
end

class UndirectedGraph(T) < Graph(T)
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges : Array(Edge2(T)))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : Edge2(T))
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 0 <= edge.to < size
    @graph[edge.from] << Edge.new(edge.to, edge.cost)
    @graph[edge.to] << Edge.new(edge.from, edge.cost)
    self
  end
end

abstract class UnweightedGraph
  getter graph : Array(Array(Int32))

  def initialize(size : Int)
    raise ArgumentError.new("Negative graph size: #{size}") unless size >= 0
    @graph = Array.new(size) { Array(Int32).new }
  end

  def add_edge(i : Int32, j : Int32)
    add_edge(UnweightedEdge2.new(i, j))
  end

  def add_edges(edges : Array(UnweightedEdge2))
    edges.each { |edge| add_edge(edge) }
    self
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  def each_edge : Nil
    (0...size).each do |v|
      graph[v].each do |u|
        yield UnweightedEdge2.new(v, u)
      end
    end
  end

  def edges
    result = [] of UnweightedEdge2
    each_edge do |edge|
      result << edge
    end
    result
  end
end

class UnweightedDirectedGraph < UnweightedGraph
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges : Array(UnweightedEdge2))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : UnweightedEdge2)
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 0 <= edge.to < size
    @graph[edge.from] << edge.to
    self
  end
end

class UnweightedUndirectedGraph < UnweightedGraph
  def initialize(size : Int)
    super
  end

  def initialize(size : Int, edges : Array(UnweightedEdge2))
    super(size)
    add_edges(edges)
  end

  def add_edge(edge : UnweightedEdge2)
    raise IndexError.new unless 0 <= edge.from < size
    raise IndexError.new unless 0 <= edge.to < size
    @graph[edge.from] << edge.to
    @graph[edge.to] << edge.from
    self
  end

  def each_child(vertex : Int, parent)
    graph[vertex].each do |u|
      yield u if u != parent
    end
  end
end

class UnweightedGraph
  def bfs(start : Int32, unreachable : U = nil) forall U
    raise ArgumentError.new unless 0 <= start < size
    queue = Deque{start}
    dist = Array(Int32?).new(size, nil)
    dist[start] = 0
    while v = queue.pop?
      graph[v].each do |edge|
        if dist[edge].nil?
          dist[edge] = dist[v].not_nil! + 1
          queue.unshift edge
        end
      end
    end
    dist.map { |i| i || unreachable }
  end

  def bfs!(start : Int32)
    bfs(start).map(&.not_nil!)
  end

  def bfs_st(start : Int32, goal : Int32, unreachable : U = nil) forall U
    raise ArgumentError.new unless 0 <= start < size
    queue = Deque{start}
    dist = Array(Int32?).new(size, nil)
    dist[start] = 0
    while v = queue.pop?
      d = dist[v].not_nil!
      return d if v == goal
      graph[v].each do |edge|
        if dist[edge].nil?
          dist[edge] = d + 1
          queue.unshift edge
        end
      end
    end
    unreachable
  end
end

n = read_line.to_i
s, t = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
stones = a + [s, t]
hash = stones.zip(0..).to_h
g = UnweightedUndirectedGraph.new(stones.size)
stones.each_with_index do |x, i|
  (0..30).each do |bit|
    if j = hash[x ^ (1 << bit)]?
      g.add_edge(i, j)
    end
  end
end
puts g.bfs_st(hash[s], hash[t]).try(&.pred) || -1
0