結果

問題 No.416 旅行会社
ユーザー S97323424S97323424
提出日時 2020-10-11 11:21:59
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 693 ms / 4,000 ms
コード長 1,851 bytes
コンパイル時間 162 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 36,272 KB
最終ジャッジ日時 2023-08-21 10:38:11
合計ジャッジ時間 7,443 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 202 ms
21,228 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 9 ms
4,376 KB
testcase_09 AC 34 ms
4,716 KB
testcase_10 AC 247 ms
21,252 KB
testcase_11 AC 234 ms
21,372 KB
testcase_12 AC 231 ms
21,240 KB
testcase_13 AC 188 ms
21,232 KB
testcase_14 AC 664 ms
36,272 KB
testcase_15 AC 646 ms
35,060 KB
testcase_16 AC 612 ms
33,992 KB
testcase_17 AC 677 ms
35,220 KB
testcase_18 AC 693 ms
35,256 KB
testcase_19 AC 498 ms
29,280 KB
testcase_20 AC 503 ms
29,760 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,q=io.read("*n","*n","*n")
local g={}
for i=1,n do
    g[i]={}
end
local INF=10^20
for i=1,m do
    local a,b=io.read("*n","*n")
    g[a][b]=INF
    g[b][a]=INF
end
for i=1,q do
    local c,d=io.read("*n","*n")
    g[c][d]=i
    g[d][c]=i
end

local pq=PairingHeap:new()
local result={INF}
for i=2,n do
    result[i]=0
end
local checked={}
pq:push(-INF,1)

while not pq:empty() do
    local cur=pq:top() pq:pop()
    if checked[cur] then
        goto continue
    end
    for nxt,v in pairs(g[cur]) do
        local val=math.min(result[cur],v)
        if result[nxt]<val then
            result[nxt]=val
            pq:push(-result[nxt],nxt)
        end
    end
    checked[cur]=true
    ::continue::
end

for i=2,n do
    print(result[i]<INF and result[i] or -1)
end
0