結果
問題 | No.1473 おでぶなおばけさん |
ユーザー |
![]() |
提出日時 | 2021-04-11 18:52:37 |
言語 | Crystal (1.14.0) |
結果 |
AC
|
実行時間 | 711 ms / 2,000 ms |
コード長 | 2,615 bytes |
コンパイル時間 | 11,251 ms |
コンパイル使用メモリ | 294,812 KB |
実行使用メモリ | 30,976 KB |
最終ジャッジ日時 | 2024-06-27 13:33:14 |
合計ジャッジ時間 | 23,857 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 47 |
ソースコード
# require "template" lib C fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64 end class String def to_i64 C.strtoll(self, nil, 10) end end # require "graph" struct Edge(T) property to : Int32 property cost : T def initialize(@to : Int32, @cost : T) end end struct Edge2(T) property from : Int32 property to : Int32 property cost : T def initialize(@from : Int32, @to : Int32, @cost : T) end end class Graph(T) getter size : Int32 getter graph : Array(Array(Edge(T))) def initialize(@size : Int32) raise ArgumentError.new("Negative graph size: #{size}") if size < 0 @graph = Array.new(size) { Array(Edge(T)).new } end def initialize(@size, edges : Array(Edge2(T)), undirected : Bool) raise ArgumentError.new("Negative graph size: #{size}") if size < 0 @graph = Array.new(size) { Array(Edge(T)).new } edges.each do |edge| @graph[edge.from] << Edge.new(edge.to, edge.cost) @graph[edge.to] << Edge.new(edge.from, edge.cost) if undirected end end def add_edge(i : Int32, j : Int32, cost : T) graph[i] << Edge(T).new(j, cost) graph[j] << Edge(T).new(i, cost) end end # require "graph/bfs" # require "./graph" class Graph(T) def bfs(start : Int32, unreachable : T = nil) forall T raise ArgumentError.new unless 0 <= start < size queue = Deque(Int32).new queue.unshift start dist = Array(Int32?).new(size, nil) dist[start] = 0 while v = queue.pop? graph[v].each do |edge| if dist[edge.to].nil? dist[edge.to] = dist[v].not_nil! + 1 queue.unshift edge.to end end end dist.map { |i| i ? i : unreachable } end def bfs_st(start : Int32, goal : Int32, unreachable : T = nil) forall T raise ArgumentError.new unless 0 <= start < size queue = Deque(Int32).new queue.unshift 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.to].nil? dist[edge.to] = d + 1 queue.unshift edge.to end end end unreachable end end n, m = read_line.split.map(&.to_i) edges = (0...m).map { s, t, d = read_line.split.map(&.to_i) Edge2(Int32).new(s - 1, t - 1, d) } ans = (1..10**9 + 1).bsearch { |weight| graph = Graph.new(n, edges.select { |edge| edge.cost >= weight }, undirected: true) graph.bfs(0).last.nil? }.not_nil! - 1 graph = Graph.new(n, edges.select { |edge| edge.cost >= ans }, undirected: true) puts [ans, graph.bfs_st(0, n - 1)].join(' ')