結果

問題 No.788 トラックの移動
ユーザー yuruhiyayuruhiya
提出日時 2021-04-08 12:04:03
言語 Crystal
(1.11.2)
結果
WA  
実行時間 -
コード長 4,228 bytes
コンパイル時間 13,433 ms
コンパイル使用メモリ 297,012 KB
実行使用メモリ 50,040 KB
最終ジャッジ日時 2024-06-23 15:59:37
合計ジャッジ時間 22,508 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,598 ms
50,012 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 355 ms
18,588 KB
testcase_05 AC 1,537 ms
49,968 KB
testcase_06 AC 1,583 ms
50,040 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 210 ms
48,160 KB
testcase_16 AC 1,292 ms
49,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

struct Edge(T)
  property to : Int32
  property cost : T

  def initialize(@to, @cost)
  end
end

class Graph(T)
  getter size : Int32
  getter graph : Array(Array(Edge(T)))

  def initialize(@size)
    @graph = Array(Array(Edge(T))).new(size) { Array(Edge(T)).new }
  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

# ac-library.cr by hakatashi https://github.com/google/ac-library.cr
#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module AtCoder
  # Implements standard priority queue like [std::priority_queue](https://en.cppreference.com/w/cpp/container/priority_queue).
  #
  # ```
  # q = AtCoder::PriorityQueue(Int64).new
  # q << 1_i64
  # q << 3_i64
  # q << 2_i64
  # q.pop # => 3
  # q.pop # => 2
  # q.pop # => 1
  # ```
  class PriorityQueue(T)
    getter heap : Array(T)

    def initialize
      initialize(&.itself)
    end

    # Initializes queue with the custom comperator.
    #
    # ```
    # q = AtCoder::PriorityQueue(Int64).new { |n| -n }
    # q << 1_i64
    # q << 3_i64
    # q << 2_i64
    # q.pop # => 1
    # q.pop # => 2
    # q.pop # => 3
    # ```
    def initialize(&block : T -> (Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64))
      @heap = Array(T).new
      @priority_proc = block
    end

    # Pushes value into the queue.
    def push(v : T)
      @heap << v
      index = @heap.size - 1
      while index != 0
        parent = (index - 1) // 2
        if @priority_proc.call(@heap[parent]) >= @priority_proc.call(@heap[index])
          break
        end
        @heap[parent], @heap[index] = @heap[index], @heap[parent]
        index = parent
      end
    end

    # Alias of `push`
    def <<(v : T)
      push(v)
    end

    # Pops value from the queue.
    def pop
      if @heap.size == 0
        return nil
      end
      if @heap.size == 1
        return @heap.pop
      end
      ret = @heap.first
      @heap[0] = @heap.pop
      index = 0
      while index * 2 + 1 < @heap.size
        child = if index * 2 + 2 < @heap.size && @priority_proc.call(@heap[index * 2 + 1]) < @priority_proc.call(@heap[index * 2 + 2])
                  index * 2 + 2
                else
                  index * 2 + 1
                end
        if @priority_proc.call(@heap[index]) >= @priority_proc.call(@heap[child])
          break
        end
        @heap[child], @heap[index] = @heap[index], @heap[child]
        index = child
      end
      ret
    end

    # Returns `true` if the queue is empty.
    delegate :empty?, to: @heap

    # Returns size of the queue.
    delegate :size, to: @heap
  end
end

class Graph(T)
  def dijkstra(start)
    que = AtCoder::PriorityQueue({Int32, T}).new { |(vertex, dist)| -dist }
    que << {start, T.zero}
    dist = Array.new(size, T::MAX)
    dist[start] = T.zero

    while vd = que.pop
      v, d = vd
      next if dist[v] < d
      graph[v].each do |edge|
        if dist[edge.to] > dist[v] + edge.cost
          dist[edge.to] = dist[v] + edge.cost
          que << {edge.to, dist[edge.to]}
        end
      end
    end
    dist
  end
end

n, m, l = read_line.split.map(&.to_i)
l -= 1
t = read_line.split.map(&.to_i64)
g = Graph(Int64).new(n)
m.times do
  a, b, c = read_line.split.map(&.to_i)
  g.add_edge(a - 1, b - 1, c.to_i64)
end

dist = (0...n).map { |i| g.dijkstra(i) }
puts (0...n).min_of { |i|
  (0...n).sum { |j| t[j] * dist[i][j] } * 2 + dist[l][i] -
    ((0...n).select { |j| t[j] > 0 }.max_of? { |j| dist[l][i] + dist[i][j] - dist[l][j] } || 0i64)
}
0