提出 #61736134


ソースコード 拡げる

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













def dijkstra(graph, start, cost: ->(c, edge) { c + edge[1] })
    s = 1 << (graph.size - 1).bit_length
    dist = [Float::INFINITY] * graph.size
    none = [Float::INFINITY, nil]
    seg = [none] * (2 * s)
    update = ->(i, x) {
        i += s
        seg[i] = x
        while i > 1
            i >>= 1
            seg[i] = seg[i * 2, 2].min
        end
    }
    
    dist[start] = 0
    update.call(start, [0, start])

    until seg[1] == none
        current_dist, current_vertex = seg[1]
        update.call(current_vertex, none)
        
        next if current_dist > dist[current_vertex]
        
        graph[current_vertex].each do |edge|
            neighbor, _ = edge
            new_dist = cost.call(current_dist, edge)
            if dist[neighbor] > new_dist
                dist[neighbor] = new_dist
                update.call(neighbor, [new_dist, neighbor])
            end
        end
    end
    
    dist
end


main

提出情報

提出日時
問題 E - Train
ユーザ magurofly
言語 Ruby (ruby 3.2.2)
得点 500
コード長 1378 Byte
結果 AC
実行時間 924 ms
メモリ 42872 KiB

ジャッジ結果

セット名 Sample All after_contest
得点 / 配点 0 / 0 500 / 500 0 / 0
結果
AC × 4
AC × 28
AC × 3
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
after_contest_01.txt AC 95 ms 17136 KiB
after_contest_02.txt AC 734 ms 37428 KiB
after_contest_03.txt AC 805 ms 40196 KiB
hand.txt AC 43 ms 17252 KiB
max_01.txt AC 888 ms 42776 KiB
random_01.txt AC 202 ms 37668 KiB
random_02.txt AC 146 ms 32984 KiB
random_05.txt AC 160 ms 25428 KiB
random_06.txt AC 167 ms 26976 KiB
random_07.txt AC 210 ms 29048 KiB
random_08.txt AC 194 ms 28728 KiB
random_11.txt AC 159 ms 25588 KiB
random_12.txt AC 135 ms 25172 KiB
random_13.txt AC 144 ms 25492 KiB
random_14.txt AC 177 ms 27492 KiB
random_15.txt AC 194 ms 28676 KiB
random_16.txt AC 168 ms 27636 KiB
random_17.txt AC 224 ms 30808 KiB
random_18.txt AC 151 ms 25588 KiB
random_21.txt AC 896 ms 42872 KiB
random_22.txt AC 924 ms 42796 KiB
random_23.txt AC 919 ms 42812 KiB
random_24.txt AC 923 ms 42824 KiB
random_31.txt AC 867 ms 38360 KiB
random_32.txt AC 895 ms 38284 KiB
random_33.txt AC 864 ms 38176 KiB
random_34.txt AC 845 ms 38228 KiB
sample_01.txt AC 43 ms 16996 KiB
sample_02.txt AC 43 ms 17256 KiB
sample_03.txt AC 44 ms 17264 KiB
sample_04.txt AC 43 ms 17276 KiB