結果

問題 No.2387 Yokan Factory
ユーザー 👑 obakyanobakyan
提出日時 2023-07-24 22:55:07
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 1,837 ms / 5,000 ms
コード長 2,776 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 5,120 KB
実行使用メモリ 16,896 KB
最終ジャッジ日時 2024-10-01 18:39:17
合計ジャッジ時間 15,828 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,248 KB
testcase_02 AC 3 ms
5,248 KB
testcase_03 AC 3 ms
5,248 KB
testcase_04 AC 3 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 3 ms
5,248 KB
testcase_07 AC 3 ms
5,248 KB
testcase_08 AC 3 ms
5,248 KB
testcase_09 AC 3 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 3 ms
5,248 KB
testcase_15 AC 1,203 ms
15,872 KB
testcase_16 AC 354 ms
15,488 KB
testcase_17 AC 1,038 ms
15,616 KB
testcase_18 AC 1,319 ms
16,896 KB
testcase_19 AC 959 ms
10,112 KB
testcase_20 AC 821 ms
10,112 KB
testcase_21 AC 1,837 ms
16,128 KB
testcase_22 AC 747 ms
9,856 KB
testcase_23 AC 1,088 ms
11,776 KB
testcase_24 AC 340 ms
9,728 KB
testcase_25 AC 687 ms
9,088 KB
testcase_26 AC 1,263 ms
14,464 KB
testcase_27 AC 1,524 ms
15,616 KB
testcase_28 AC 9 ms
5,248 KB
testcase_29 AC 12 ms
5,248 KB
testcase_30 AC 10 ms
5,248 KB
testcase_31 AC 9 ms
5,248 KB
testcase_32 AC 7 ms
5,248 KB
testcase_33 AC 6 ms
5,248 KB
testcase_34 AC 11 ms
5,248 KB
testcase_35 AC 8 ms
5,248 KB
testcase_36 AC 6 ms
5,248 KB
testcase_37 AC 3 ms
5,248 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 = {} -- use 1D Tree (stage[], not 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, force)
  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])
    if not force and self.stage[dst] ~= self.stage[idx] then break end
    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, x = io.read("*n", "*n", "*n")
local a, b, c, d = {}, {}, {}, {}
local edge = {}
for i = 1, n do
  edge[i] = {}
end
for i = 1, m do
  a[i], b[i], c[i], d[i] = io.read("*n", "*n", "*n", "*n")
  table.insert(edge[a[i]], i)
  table.insert(edge[b[i]], i)
end

local inf = 1000000007 * 100001
x = mmi(x, inf - 1)

local len = {}
local asked = {}

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
for i = 1, n do
  len[i] = inf
  asked[i] = false
end
len[1] = 0
asked[n + 1] = true
local st = SegTree.new(n, mergefunc, n + 1)

local function solve(limit)
  for i = 1, n do
    len[i] = inf
    asked[i] = false
  end
  len[1] = 0
  asked[n + 1] = true
  st:updateAll()
  for i = 1, n do
    local src = st.stage[1]
    if asked[src] then break end
    if inf <= len[src] then break end
    asked[src] = true
    st:update(src, true)
    for j = 1, #edge[src] do
      local ei = edge[src][j]
      if limit <= d[ei] then
        local dst = a[ei] + b[ei] - src
        local cost = c[ei]
        if len[src] + cost < len[dst] then
          len[dst] = len[src] + cost
          st:update(dst)
        end
      end
    end
  end
  return len[n] < inf and len[n] <= x
end
local ok, ng = 0, 1000000001
while 1 < ng - ok do
  local mid = mfl((ng + ok) / 2)
  if solve(mid) then
    ok = mid
  else
    ng = mid
  end
end
if ok == 0 then
  print(-1)
else
  print(ok)
end
0