結果

問題 No.1094 木登り / Climbing tree
ユーザー S97323424
提出日時 2020-10-18 14:38:14
言語 Lua
(LuaJit 2.1.1734355927)
結果
AC  
実行時間 1,488 ms / 2,000 ms
コード長 2,888 bytes
コンパイル時間 718 ms
コンパイル使用メモリ 5,632 KB
実行使用メモリ 117,192 KB
最終ジャッジ日時 2024-11-08 07:09:49
合計ジャッジ時間 29,273 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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 INF=10^20

local function Dijkstra(graph,start)
    local dist={}
    local rank={0}
    local par={}
    for i=1,#graph do
        dist[i]=INF
        par[i]=start
    end
    local pq=PairingHeap:new()
    dist[start]=0
    pq:push(0,{0,start})
    
    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
                rank[to]=rank[from]+1
                par[to]=from
                pq:push(dist[to],{dist[to],to})
            end
        end
        ::continue::
    end
    
    return dist,rank,par
end

----------

local n=io.read("*n")
local tree={}
for i=1,n do
    tree[i]={}
end
for i=1,n-1 do
    local a,b,c=io.read("*n","*n","*n")
    tree[a][b]=c
    tree[b][a]=c
end
local root=1
local dist
local rank
local par={}
dist,rank,par[1]=Dijkstra(tree,root)

local bit=require("bit")
local k=1
while bit.lshift(1,k)<n do
    k=k+1
end
for i=2,k do
    par[i]={}
    for j=1,n do
        par[i][j]=root
    end
end
for i=1,k-1 do
    for j=1,n do
        if par[i][j]~=root then
            par[i+1][j]=par[i][par[i][j]]
        end
    end
end

local q=io.read("*n")
for i=1,q do
    local s,t=io.read("*n","*n")
    local length=dist[s]+dist[t]
    if rank[s]<rank[t] then
        s,t=t,s
    end
    for j=1,k do
        if bit.band(bit.rshift((rank[s]-rank[t]),(j-1)),1)>0 then
            s=par[j][s]
        end
    end
    if s~=t then
        for j=k,1,-1 do
            if par[j][s]~=par[j][t] then
                s=par[j][s]
                t=par[j][t]
            end
        end
        s=par[1][s]
    end
    io.write(length-2*dist[s].."\n")
end

0