結果

問題 No.1473 おでぶなおばけさん
ユーザー yuruhiyayuruhiya
提出日時 2021-04-11 18:45:51
言語 Crystal
(1.11.2)
結果
WA  
実行時間 -
コード長 2,457 bytes
コンパイル時間 22,264 ms
コンパイル使用メモリ 257,076 KB
実行使用メモリ 31,104 KB
最終ジャッジ日時 2023-09-09 20:50:00
合計ジャッジ時間 29,627 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 3 ms
4,456 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 518 ms
20,032 KB
testcase_08 AC 666 ms
23,144 KB
testcase_09 AC 554 ms
27,800 KB
testcase_10 AC 48 ms
10,692 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 29 ms
7,744 KB
testcase_14 AC 22 ms
6,828 KB
testcase_15 AC 40 ms
8,456 KB
testcase_16 AC 46 ms
10,656 KB
testcase_17 AC 6 ms
5,232 KB
testcase_18 AC 9 ms
5,204 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 41 ms
8,868 KB
testcase_42 AC 41 ms
8,976 KB
testcase_43 AC 165 ms
17,432 KB
testcase_44 AC 166 ms
17,384 KB
testcase_45 AC 165 ms
17,324 KB
testcase_46 AC 300 ms
18,284 KB
testcase_47 AC 370 ms
25,664 KB
testcase_48 AC 356 ms
19,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 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

  def bfs_st(start : Int32, goal : 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?
      return dist[v].not_nil! if v == goal
      graph[v].each do |edge|
        if dist[edge.to].nil?
          dist[edge.to] = dist[v].not_nil! + 1
          queue << 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(' ')
0