Submission #61679977


Source Code Expand

def main
  n, m, x, y = gets.split.map(&:to_i)
  
  graph = Array.new(n) { [] }
  m.times do
    a, b, t, k = gets.split.map(&:to_i)
    graph[a - 1] << [b - 1, t, k]
    graph[b - 1] << [a - 1, t, k]
  end
  
  dist = dijkstra(graph, x - 1, cost:->(c, edge) { (c+edge[2]-1)/edge[2]*edge[2] + edge[1]}) 
  puts dist[y - 1] == Float::INFINITY ? -1 : dist[y - 1]
end













class MaxHeap
  def initialize
    @heap = []
  end

  def push(value)
    @heap << value
    heapify_up(@heap.size - 1)
  end

  def <<(value)
    push(value)
  end

  def peek
    @heap[0]
  end
  
  def empty?
    @heap.empty?
  end

  def pop
    return nil if @heap.empty?
    return @heap.pop if @heap.size == 1

    top = @heap[0]
    @heap[0] = @heap.pop
    heapify_down(0)
    top
  end

  private

  def heapify_up(child_index)
    return if child_index.zero?

    parent_index = (child_index - 1) / 2
    return if (@heap[parent_index] <=> @heap[child_index]) >= 0

    @heap[parent_index], @heap[child_index] = @heap[child_index], @heap[parent_index]
    heapify_up(parent_index)
  end

  def heapify_down(parent_index)
    left_child_index = parent_index * 2 + 1
    right_child_index = parent_index * 2 + 2
    largest = parent_index

    largest = left_child_index if left_child_index < @heap.size && (@heap[largest] <=> @heap[left_child_index])<0
    largest = right_child_index if right_child_index < @heap.size && (@heap[largest] <=> @heap[right_child_index])<0

    return if largest == parent_index

    @heap[parent_index], @heap[largest] = @heap[largest], @heap[parent_index]
    heapify_down(largest)
  end
end


def dijkstra(graph, start, cost: ->(c, edge) { c + edge[1] })
  dist = [Float::INFINITY] * graph.size
  dist[start] = 0

  heap = MaxHeap.new
  heap.push([-dist[start], start])
  until heap.empty?
    neg_d, current_vertex = heap.pop
    current_dist = -neg_d
    
    next if current_dist > dist[current_vertex]

    graph[current_vertex].each do |edge|
      neighbor, _ = edge
      new_dist  = cost.call(current_dist, edge)
      if new_dist < dist[neighbor]
        dist[neighbor] = new_dist
        heap.push([-new_dist, neighbor])
      end
    end
  end

  dist
end


main

Submission Info

Submission Time
Task E - Train
User zeronosu77108
Language Ruby (ruby 3.2.2)
Score 500
Code Size 2307 Byte
Status AC
Exec Time 915 ms
Memory 35488 KiB

Judge Result

Set Name Sample All after_contest
Score / Max Score 0 / 0 500 / 500 0 / 0
Status
AC × 4
AC × 28
AC × 3
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All hand.txt, max_01.txt, random_01.txt, random_02.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
after_contest after_contest_01.txt, after_contest_02.txt, after_contest_03.txt
Case Name Status Exec Time Memory
after_contest_01.txt AC 143 ms 17396 KiB
after_contest_02.txt AC 913 ms 35012 KiB
after_contest_03.txt AC 915 ms 35008 KiB
hand.txt AC 46 ms 17148 KiB
max_01.txt AC 335 ms 35380 KiB
random_01.txt AC 257 ms 35476 KiB
random_02.txt AC 182 ms 30992 KiB
random_05.txt AC 190 ms 25696 KiB
random_06.txt AC 207 ms 26924 KiB
random_07.txt AC 273 ms 28660 KiB
random_08.txt AC 250 ms 28812 KiB
random_11.txt AC 193 ms 25672 KiB
random_12.txt AC 166 ms 25232 KiB
random_13.txt AC 169 ms 25236 KiB
random_14.txt AC 217 ms 27316 KiB
random_15.txt AC 249 ms 28328 KiB
random_16.txt AC 214 ms 27008 KiB
random_17.txt AC 284 ms 30864 KiB
random_18.txt AC 184 ms 25356 KiB
random_21.txt AC 339 ms 35364 KiB
random_22.txt AC 337 ms 35456 KiB
random_23.txt AC 327 ms 35332 KiB
random_24.txt AC 332 ms 35488 KiB
random_31.txt AC 746 ms 34652 KiB
random_32.txt AC 764 ms 34916 KiB
random_33.txt AC 746 ms 34984 KiB
random_34.txt AC 727 ms 34816 KiB
sample_01.txt AC 45 ms 17176 KiB
sample_02.txt AC 46 ms 17280 KiB
sample_03.txt AC 46 ms 17248 KiB
sample_04.txt AC 45 ms 17064 KiB