結果

問題 No.17 2つの地点に泊まりたい
ユーザー 👑 obakyanobakyan
提出日時 2019-06-09 10:31:56
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 31 ms / 5,000 ms
コード長 1,311 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 6,944 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 20:03:58
合計ジャッジ時間 1,285 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 17 ms
5,376 KB
testcase_06 AC 12 ms
5,376 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 30 ms
5,376 KB
testcase_09 AC 31 ms
5,376 KB
testcase_10 AC 9 ms
5,376 KB
testcase_11 AC 18 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 16 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl = math.floor
local inf = 10000000
local n = io.read("*n")
local staycost = {}
for i = 1, n do staycost[i] = io.read("*n") end
local m = io.read("*n")
local line = {}
for i = 1, n do line[i] = {} end
for i = 1, m do
  local a, b, c = io.read("*n", "*n", "*n")
  a, b = a + 1, b + 1
  line[a][b] = c
  line[b][a] = c
end
local cost = {}
for i = 1, n * n do
  cost[i] = inf
end
cost[1] = 0
local tasks = {1}
local tasknum, done = 1, 0

local function walk(src, dst, c)
  if cost[src] + c < cost[dst] then
    -- print("from " .. src .. " to " .. dst .. " by " .. cost[src] + c)
    cost[dst] = cost[src] + c
    table.insert(tasks, dst)
    tasknum = tasknum + 1
  end
end

while done < tasknum do
  done = done + 1
  local curidx = tasks[done]
  local cur_stay_city = 1 + mfl((curidx - 1) / n)
  local cur_city = curidx - (cur_stay_city - 1) * n
  for dstcity, c in pairs(line[cur_city]) do
    walk(curidx, dstcity + (cur_stay_city - 1) * n, c)
    if 1 < cur_city and cur_city < n then
      if cur_stay_city == 1 then
        walk(curidx, dstcity + (cur_city - 1) * n, c + staycost[cur_city])
      elseif cur_stay_city < n then
        if cur_stay_city ~= cur_city then
          walk(curidx, dstcity + (n - 1) * n, c + staycost[cur_city])
        end
      end
    end
  end
end
print(cost[n * n])
0