結果

問題 No.807 umg tours
ユーザー 👑 obakyanobakyan
提出日時 2020-09-12 00:05:12
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,156 ms / 4,000 ms
コード長 2,268 bytes
コンパイル時間 114 ms
コンパイル使用メモリ 5,296 KB
実行使用メモリ 68,264 KB
最終ジャッジ日時 2023-08-27 23:29:15
合計ジャッジ時間 13,026 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 612 ms
46,172 KB
testcase_12 AC 675 ms
44,728 KB
testcase_13 AC 872 ms
58,080 KB
testcase_14 AC 368 ms
27,060 KB
testcase_15 AC 284 ms
21,536 KB
testcase_16 AC 902 ms
60,900 KB
testcase_17 AC 1,152 ms
67,252 KB
testcase_18 AC 1,156 ms
68,264 KB
testcase_19 AC 1,129 ms
68,220 KB
testcase_20 AC 765 ms
44,676 KB
testcase_21 AC 777 ms
45,828 KB
testcase_22 AC 302 ms
21,632 KB
testcase_23 AC 237 ms
18,672 KB
testcase_24 AC 607 ms
46,304 KB
testcase_25 AC 1,105 ms
65,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local pow2 = {1}
for i = 2, 28 do pow2[i] = pow2[i - 1] * 2 end
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = pow2[i]
    for j = 0, cnt - 1 do
      self.stage[cnt + j] = self.func(self.stage[(cnt + j) * 2], self.stage[(cnt + j) * 2 + 1])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
  end
  self.stagenum = stagenum
  for i = 1, mul * 2 - 1 do self.stage[i] = emptyvalue end
  for i = 1, n do self.stage[mul + i - 1] = i end
  self:updateAll()
end
SegTree.update = function(self, idx)
  idx = idx + pow2[self.stagenum] - 1
  for i = self.stagenum - 1, 1, -1 do
    local dst = mfl(idx / 2)
    local rem = dst * 4 + 1 - idx
    self.stage[dst] = self.func(self.stage[idx], self.stage[rem])
    idx = dst
  end
end
SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, m = io.read("*n", "*n")
local rawn = n
n = n * 2
local len = {}
local asked = {}
local edge = {}
for i = 1, n do
  len[i] = -1
  asked[i] = false
  edge[i] = {}
  if i <= rawn then edge[i][rawn + i] = 0 end
end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  edge[a][b], edge[b][a] = c, c
  edge[rawn + a][rawn + b], edge[rawn + b][rawn + a] = c, c
  edge[a][rawn + b], edge[b][rawn + a] = 0, 0
end
len[1] = 0
asked[n + 1] = true

local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] then return x
  elseif len[x] < 0 then return y
  elseif len[y] < 0 then return x
  else
    return len[x] < len[y] and x or y
  end
end

local st = SegTree.new(n, mergefunc, n + 1)

for i = 1, n do
  local src = st.stage[1]
  if asked[src] then break end
  if len[src] < 0 then break end
  asked[src] = true
  st:update(src)
  for dst, cost in pairs(edge[src]) do
    if len[dst] < 0 or len[src] + cost < len[dst] then
      len[dst] = len[src] + cost
      st:update(dst)
    end
  end
end

for i = 1, rawn do
  print(len[i] + len[rawn + i])
end
0