結果

問題 No.1065 電柱 / Pole (Easy)
ユーザー 👑 obakyanobakyan
提出日時 2020-05-29 22:05:33
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 643 ms / 2,000 ms
コード長 2,176 bytes
コンパイル時間 95 ms
コンパイル使用メモリ 6,684 KB
実行使用メモリ 45,056 KB
最終ジャッジ日時 2024-04-23 22:37:31
合計ジャッジ時間 10,943 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 307 ms
24,064 KB
testcase_03 AC 343 ms
39,296 KB
testcase_04 AC 342 ms
39,296 KB
testcase_05 AC 333 ms
39,168 KB
testcase_06 AC 333 ms
39,296 KB
testcase_07 AC 110 ms
13,312 KB
testcase_08 AC 436 ms
41,856 KB
testcase_09 AC 38 ms
7,040 KB
testcase_10 AC 199 ms
21,120 KB
testcase_11 AC 124 ms
14,208 KB
testcase_12 AC 42 ms
6,940 KB
testcase_13 AC 266 ms
24,732 KB
testcase_14 AC 323 ms
27,668 KB
testcase_15 AC 372 ms
29,468 KB
testcase_16 AC 158 ms
14,848 KB
testcase_17 AC 396 ms
31,024 KB
testcase_18 AC 114 ms
12,160 KB
testcase_19 AC 398 ms
30,856 KB
testcase_20 AC 101 ms
12,596 KB
testcase_21 AC 132 ms
13,184 KB
testcase_22 AC 363 ms
29,748 KB
testcase_23 AC 5 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 37 ms
6,940 KB
testcase_26 AC 184 ms
15,484 KB
testcase_27 AC 176 ms
15,636 KB
testcase_28 AC 322 ms
25,680 KB
testcase_29 AC 35 ms
6,940 KB
testcase_30 AC 368 ms
26,516 KB
testcase_31 AC 253 ms
21,852 KB
testcase_32 AC 161 ms
14,252 KB
testcase_33 AC 339 ms
25,012 KB
testcase_34 AC 113 ms
10,736 KB
testcase_35 AC 364 ms
26,336 KB
testcase_36 AC 5 ms
6,940 KB
testcase_37 AC 6 ms
6,940 KB
testcase_38 AC 5 ms
6,944 KB
testcase_39 AC 7 ms
6,940 KB
testcase_40 AC 3 ms
6,940 KB
testcase_41 AC 643 ms
45,056 KB
testcase_42 AC 159 ms
14,976 KB
testcase_43 AC 298 ms
24,704 KB
testcase_44 AC 96 ms
12,032 KB
testcase_45 AC 280 ms
24,320 KB
testcase_46 AC 1 ms
6,944 KB
testcase_47 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local msq = math.sqrt
-- use a 1D Seg class ( stage[], not stage[][])
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 x, y = io.read("*n", "*n")
local p, q = {}, {}
local edge = {}
for i = 1, n do
  p[i], q[i] = io.read("*n", "*n")
  edge[i] = {}
end
for i = 1, m do
  local a, b = io.read("*n", "*n")
  local len = msq((p[a] - p[b]) * (p[a] - p[b]) + (q[a] - q[b]) * (q[a] - q[b]))
  edge[a][b], edge[b][a] = len, len
end

local inf = 10^18
local len = {}
local asked = {}
for i = 1, n do
  len[i] = inf
  asked[i] = false
end
len[x] = 0
asked[n + 1] = true

local function mergefunc(x, y)
  if asked[x] then return y
  elseif asked[y] 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]
  asked[src] = true
  st:update(src)
  for dst, cost in pairs(edge[src]) do
    if len[src] + cost < len[dst] then
      len[dst] = len[src] + cost
      st:update(dst)
    end
  end
end

print(len[y])
0