結果

問題 No.1473 おでぶなおばけさん
ユーザー yuruhiyayuruhiya
提出日時 2021-04-11 18:52:37
言語 Crystal
(1.11.2)
結果
AC  
実行時間 683 ms / 2,000 ms
コード長 2,615 bytes
コンパイル時間 19,702 ms
コンパイル使用メモリ 256,844 KB
実行使用メモリ 30,212 KB
最終ジャッジ日時 2023-09-09 21:03:03
合計ジャッジ時間 33,832 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,412 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 546 ms
22,428 KB
testcase_03 AC 303 ms
15,596 KB
testcase_04 AC 267 ms
14,180 KB
testcase_05 AC 86 ms
8,480 KB
testcase_06 AC 402 ms
16,460 KB
testcase_07 AC 554 ms
20,084 KB
testcase_08 AC 683 ms
30,212 KB
testcase_09 AC 611 ms
27,692 KB
testcase_10 AC 49 ms
10,636 KB
testcase_11 AC 49 ms
8,992 KB
testcase_12 AC 50 ms
10,840 KB
testcase_13 AC 30 ms
7,644 KB
testcase_14 AC 23 ms
6,740 KB
testcase_15 AC 41 ms
8,592 KB
testcase_16 AC 48 ms
10,672 KB
testcase_17 AC 6 ms
5,168 KB
testcase_18 AC 9 ms
5,400 KB
testcase_19 AC 73 ms
8,972 KB
testcase_20 AC 196 ms
16,908 KB
testcase_21 AC 153 ms
15,288 KB
testcase_22 AC 132 ms
18,144 KB
testcase_23 AC 123 ms
16,200 KB
testcase_24 AC 124 ms
17,300 KB
testcase_25 AC 581 ms
25,480 KB
testcase_26 AC 603 ms
19,348 KB
testcase_27 AC 150 ms
9,452 KB
testcase_28 AC 475 ms
24,460 KB
testcase_29 AC 207 ms
18,352 KB
testcase_30 AC 325 ms
18,564 KB
testcase_31 AC 383 ms
19,184 KB
testcase_32 AC 261 ms
19,084 KB
testcase_33 AC 219 ms
21,280 KB
testcase_34 AC 114 ms
13,064 KB
testcase_35 AC 81 ms
9,580 KB
testcase_36 AC 192 ms
14,088 KB
testcase_37 AC 365 ms
18,900 KB
testcase_38 AC 50 ms
7,696 KB
testcase_39 AC 48 ms
10,548 KB
testcase_40 AC 48 ms
10,540 KB
testcase_41 AC 42 ms
8,888 KB
testcase_42 AC 42 ms
9,004 KB
testcase_43 AC 153 ms
17,492 KB
testcase_44 AC 170 ms
17,524 KB
testcase_45 AC 168 ms
17,508 KB
testcase_46 AC 316 ms
18,128 KB
testcase_47 AC 414 ms
19,464 KB
testcase_48 AC 352 ms
19,732 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)
    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(' ')
0