結果
| 問題 | No.17 2つの地点に泊まりたい | 
| コンテスト | |
| ユーザー | 👑 | 
| 提出日時 | 2019-06-09 10:31:56 | 
| 言語 | Lua (LuaJit 2.1.1734355927) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 27 ms / 5,000 ms | 
| コード長 | 1,311 bytes | 
| コンパイル時間 | 167 ms | 
| コンパイル使用メモリ | 6,816 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-05 23:59:55 | 
| 合計ジャッジ時間 | 1,259 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
ソースコード
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])
            
            
            
        