結果
問題 | No.2565 はじめてのおつかい |
ユーザー | ngng628 |
提出日時 | 2023-12-02 16:12:28 |
言語 | Crystal (1.11.2) |
結果 |
AC
|
実行時間 | 274 ms / 2,000 ms |
コード長 | 2,922 bytes |
コンパイル時間 | 11,881 ms |
コンパイル使用メモリ | 283,860 KB |
実行使用メモリ | 56,564 KB |
最終ジャッジ日時 | 2023-12-02 16:12:49 |
合計ジャッジ時間 | 16,843 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,548 KB |
testcase_01 | AC | 1 ms
6,548 KB |
testcase_02 | AC | 2 ms
6,548 KB |
testcase_03 | AC | 274 ms
56,564 KB |
testcase_04 | AC | 111 ms
56,564 KB |
testcase_05 | AC | 1 ms
6,548 KB |
testcase_06 | AC | 78 ms
19,012 KB |
testcase_07 | AC | 37 ms
9,856 KB |
testcase_08 | AC | 39 ms
13,632 KB |
testcase_09 | AC | 64 ms
14,336 KB |
testcase_10 | AC | 48 ms
16,084 KB |
testcase_11 | AC | 141 ms
38,400 KB |
testcase_12 | AC | 22 ms
6,548 KB |
testcase_13 | AC | 44 ms
14,524 KB |
testcase_14 | AC | 89 ms
26,436 KB |
testcase_15 | AC | 116 ms
25,912 KB |
testcase_16 | AC | 131 ms
50,932 KB |
testcase_17 | AC | 19 ms
9,984 KB |
testcase_18 | AC | 38 ms
21,684 KB |
testcase_19 | AC | 125 ms
33,152 KB |
testcase_20 | AC | 116 ms
29,988 KB |
testcase_21 | AC | 107 ms
30,812 KB |
testcase_22 | AC | 54 ms
20,896 KB |
testcase_23 | AC | 61 ms
20,496 KB |
testcase_24 | AC | 13 ms
6,784 KB |
testcase_25 | AC | 112 ms
32,384 KB |
testcase_26 | AC | 61 ms
20,556 KB |
testcase_27 | AC | 109 ms
34,456 KB |
testcase_28 | AC | 123 ms
30,764 KB |
testcase_29 | AC | 155 ms
40,696 KB |
testcase_30 | AC | 119 ms
36,632 KB |
testcase_31 | AC | 114 ms
32,420 KB |
testcase_32 | AC | 78 ms
19,920 KB |
testcase_33 | AC | 105 ms
35,376 KB |
testcase_34 | AC | 112 ms
27,788 KB |
testcase_35 | AC | 124 ms
43,252 KB |
testcase_36 | AC | 125 ms
34,940 KB |
testcase_37 | AC | 59 ms
21,156 KB |
testcase_38 | AC | 92 ms
28,072 KB |
testcase_39 | AC | 90 ms
25,520 KB |
testcase_40 | AC | 133 ms
39,748 KB |
testcase_41 | AC | 178 ms
44,060 KB |
testcase_42 | AC | 79 ms
19,084 KB |
testcase_43 | AC | 82 ms
27,080 KB |
testcase_44 | AC | 37 ms
13,556 KB |
testcase_45 | AC | 22 ms
12,124 KB |
testcase_46 | AC | 96 ms
25,080 KB |
testcase_47 | AC | 57 ms
14,328 KB |
testcase_48 | AC | 65 ms
12,928 KB |
testcase_49 | AC | 22 ms
6,548 KB |
testcase_50 | AC | 62 ms
12,924 KB |
testcase_51 | AC | 1 ms
6,548 KB |
testcase_52 | AC | 36 ms
7,808 KB |
ソースコード
main = -> { n, m = read_line.split.map &.to_i64 graph = NgLib::BfsGraph.new(3 * n) graph2 = NgLib::BfsGraph.new(3 * n) m.times do u, v = read_line.split.map &.to_i64.pred graph.add_edge(u, v, directed: true) graph.add_edge(u + n, v + n, directed: true) graph.add_edge(u + 2*n, v + 2*n, directed: true) graph2.add_edge(u, v, directed: true) graph2.add_edge(u + n, v + n, directed: true) graph2.add_edge(u + 2*n, v + 2*n, directed: true) end graph.add_edge(n - 2, n - 2 + n, directed: true) graph.add_edge(n - 1 + n, n - 1 + 2*n, directed: true) graph2.add_edge(n - 1, n - 1 + n, directed: true) graph2.add_edge(n - 2 + n, n - 2 + 2*n, directed: true) d = Math.min(graph.shortest_path(0, 0 + 2*n), graph2.shortest_path(0, 0 + 2*n)) puts d >= OO ? -1 : d - 2 } main.call OO = (1_i64 << 62) - (1_i64 << 31) module NgLib # $n$ 頂点 $m$ 辺からなるグラフに対して、幅優先探索によって最短経路を求めます。 # # 経路の復元も可能です。 class BfsGraph getter size : Int32 getter graph : Array(Array(Int32)) # $n$ 頂点 $0$ 辺からなるグラフを作成します。 # # ``` # graph = BfsGraph.new(n) # ``` def initialize(n : Int) @size = n.to_i64.to_i32 @graph = Array.new(@size) { Array(Int32).new } end # 辺 $(u, v)$ を追加します。 # # `directed` が `true` の場合、 # 有向辺とみなして、$u$ から $v$ への辺のみ生やします。 # # ``` # graph = BfsGraph.new(n) # graph.add_edge(u, v) # => (u) <---w---> (v) # graph.add_edge(u, v, directed: true) # => (u) ----w---> (v) # ``` def add_edge(u : Int, v : Int, directed : Bool = false) @graph[u.to_i32] << v.to_i32 @graph[v.to_i32] << u.to_i32 unless directed end # 全点対間の最短経路長を返します。 # # ``` # dists = graph.shortest_path # dists # => [[0, 1, 3], [1, 0, 2], [1, 1, 0]] # ``` def shortest_path (0...@size).map { |s| shortest_path(s) } end # 始点 `start` から各頂点への最短経路長を返します。 # # ``` # dist = graph.shortest_path(start: 2) # dist # => [3, 8, 0, 7, 1] # ``` def shortest_path(start : Int) queue = Deque.new([start.to_i32]) dist = Array.new(@size) { |i| i == start ? 0_i64 : OO } until queue.empty? from = queue.shift @graph[from].each do |to| next if dist[to] != OO dist[to] = dist[from] + 1 queue << to end end dist end # 始点 `start` から終点 `dest` への最短経路長を返します。 # # ``` # dist = graph.shortest_path(start: 2, dest: 0) # dist # => 12 # ``` def shortest_path(start : Int, dest : Int) shortest_path(start)[dest] end end end