結果
問題 |
No.1473 おでぶなおばけさん
|
ユーザー |
![]() |
提出日時 | 2021-04-11 18:17:55 |
言語 | Crystal (1.14.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,947 bytes |
コンパイル時間 | 12,228 ms |
コンパイル使用メモリ | 296,100 KB |
実行使用メモリ | 31,304 KB |
最終ジャッジ日時 | 2024-06-27 12:55:31 |
合計ジャッジ時間 | 24,606 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 WA * 29 |
ソースコード
# 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) @graph = Array.new(size) { Array(Edge(T)).new } end def initialize(@size, edges : Array(Edge2(T)), undirected : Bool) @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 << 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 << edge.to end end end dist.map { |i| i ? i : 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 }, true) graph.bfs(0).last.nil? }.not_nil! - 1 graph = Graph.new(n, edges.select { |edge| edge.cost >= ans }, true) puts [ans, graph.bfs(0).last.not_nil!].join(' ')