結果

問題 No.160 最短経路のうち辞書順最小
ユーザー S97323424S97323424
提出日時 2020-10-19 21:14:01
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 2,079 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 5,216 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 13:26:53
合計ジャッジ時間 1,721 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 11 ms
4,376 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 8 ms
4,380 KB
testcase_12 AC 7 ms
4,376 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 7 ms
4,376 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 7 ms
4,380 KB
testcase_19 AC 7 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 5 ms
4,380 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 7 ms
4,380 KB
testcase_24 AC 7 ms
4,380 KB
testcase_25 AC 6 ms
4,380 KB
testcase_26 AC 7 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 18 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local function meld(node1,node2)
    if not node1 then
        return node2
    elseif not node2 then
        return node1
    elseif node1[1]<node2[1] then
        node2[4]=node1[3]
        node1[3]=node2
        return node1
    else
        node1[4]=node2[3]
        node2[3]=node1
        return node2
    end
end

local function pairing(node,stack)
    if node and node[4] then
        return pairing(node[4][4],rawset(meld(node,node[4]),4,stack))
    else
        return stack,node
    end
end

local function meld_pairs(stack,node)
    if stack then
        return meld_pairs(stack[4],meld(stack,node))
    else
        return node
    end
end

local PairingHeap={}

PairingHeap.empty=function(self)
    return not self[1]
end

PairingHeap.push=function(self,key,value)
    self[1]=meld(self[1],{key,value,false,false})
end

PairingHeap.top=function(self)
    return self[1][2]
end

PairingHeap.pop=function(self)
    self[1]=meld_pairs(pairing(self[1][3],false))
end

PairingHeap.new=function()
    return setmetatable({false},{__index=PairingHeap})
end

----------

local n,m,s,g=io.read("*n","*n","*n","*n")
local graph={}
for i=0,n-1 do
    graph[i]={}
end
for i=1,m do
    local a,b,c=io.read("*n","*n","*n")
    graph[a][b]=c
    graph[b][a]=c
end

local INF=10^20
local dist={}
local prev={}
for i=0,n-1 do
    dist[i]=INF
    prev[i]=PairingHeap:new()
end
local pq=PairingHeap:new()
dist[g]=0
pq:push(0,{0,g})

while not pq:empty() do
    local p=pq:top() pq:pop()
    local from=p[2]
    if dist[from]<p[1] then
        goto continue
    end
    for to,cost in pairs(graph[from]) do
        if dist[to]>dist[from]+cost then
            dist[to]=dist[from]+cost
            prev[to]=PairingHeap:new()
            prev[to]:push(from,from)
            pq:push(dist[to],{dist[to],to})
        elseif dist[to]==dist[from]+cost then
            prev[to]:push(from,from)
        end
    end
    ::continue::
end

local path={s}
while path[#path]~=g do
    local cur=path[#path]
    local next=prev[cur]:top()
    table.insert(path,next)
end
print(table.concat(path," "))
0