結果

問題 No.1301 Strange Graph Shortest Path
ユーザー 👑 obakyanobakyan
提出日時 2020-11-28 14:21:44
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 7,569 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 5,176 KB
実行使用メモリ 61,212 KB
最終ジャッジ日時 2023-10-09 23:57:49
合計ジャッジ時間 26,212 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,748 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 619 ms
47,940 KB
testcase_03 AC 506 ms
40,220 KB
testcase_04 AC 723 ms
59,568 KB
testcase_05 AC 561 ms
43,704 KB
testcase_06 AC 700 ms
54,704 KB
testcase_07 AC 609 ms
48,364 KB
testcase_08 AC 496 ms
40,492 KB
testcase_09 AC 624 ms
53,072 KB
testcase_10 AC 525 ms
40,844 KB
testcase_11 AC 661 ms
55,052 KB
testcase_12 AC 685 ms
56,132 KB
testcase_13 AC 616 ms
48,324 KB
testcase_14 AC 597 ms
47,460 KB
testcase_15 AC 596 ms
47,276 KB
testcase_16 AC 720 ms
60,176 KB
testcase_17 AC 642 ms
50,100 KB
testcase_18 AC 593 ms
46,168 KB
testcase_19 AC 666 ms
55,500 KB
testcase_20 AC 668 ms
56,656 KB
testcase_21 AC 646 ms
49,732 KB
testcase_22 AC 678 ms
58,332 KB
testcase_23 AC 666 ms
49,120 KB
testcase_24 AC 656 ms
56,456 KB
testcase_25 AC 730 ms
58,064 KB
testcase_26 AC 627 ms
49,372 KB
testcase_27 AC 648 ms
50,412 KB
testcase_28 AC 547 ms
43,216 KB
testcase_29 AC 729 ms
61,212 KB
testcase_30 AC 709 ms
57,008 KB
testcase_31 AC 711 ms
58,060 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 350 ms
45,996 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mmi, mma = math.min, math.max
local MinCostFlow = {}

MinCostFlow.initialize = function(self, n, spos, tpos, inf)
  self.n = n
  self.spos, self.tpos = spos, tpos
  self.inf = inf
  -- edge_dst[src][i] := dst
  self.edge_dst = {}
  -- edge_cost[src][i] := cost from src to edge_dst[src][i]
  self.edge_cost = {}
  -- edge_cap[src][i] := capacity from src to edge_dst[src][i]
  self.edge_cap = {}
  -- edge_dst_invedge_idx[src][i] := "j" where edge_dst[dst][j] == src
  -- in this case, edge_dst_invedge_idx[dst][j] should be "i".
  self.edge_dst_invedge_idx = {}
  -- len[v] := length from spos. len[spos] := 0
  self.len = {}
  -- sub_graph_flag[v] := temporal flag to restore shortest path
  self.sub_graph_flag = {}
  -- sub_graph_v[i] := list of vertexes that are contained in the sub-graph. from tpos to spos.
  self.sub_graph_v = {}
  -- sub_graph_edgeidx[i] := edge index from sub_graph_v[i + 1] to sub_graph_v[i]
  self.sub_graph_edgeidx = {}
  -- sub_graph_size := the size of sub_graph_v.
  -- may not equal to #sub_graph_v (because not cleared).
  self.sub_graph_size = 0
  for i = 1, n do
    self.edge_dst[i] = {}
    self.edge_cost[i] = {}
    self.edge_cap[i] = {}
    self.edge_dst_invedge_idx[i] = {}
    self.len[i] = 0
    self.sub_graph_flag[i] = false
  end
end

MinCostFlow.addEdge = function(self, src, dst, cost, cap)
  table.insert(self.edge_dst[src], dst)
  table.insert(self.edge_cost[src], cost)
  table.insert(self.edge_cap[src], cap)
  table.insert(self.edge_dst_invedge_idx[src], 1 + #self.edge_dst[dst])
  table.insert(self.edge_dst[dst], src)
  table.insert(self.edge_cost[dst], -cost)
  table.insert(self.edge_cap[dst], 0)--invcap
  table.insert(self.edge_dst_invedge_idx[dst], #self.edge_dst[src])
end
MinCostFlow.invwalk_recursive = function(self)
  local edge_cap, edge_cost = self.edge_cap, self.edge_cost
  local len = self.len
  local sub_graph_flag = self.sub_graph_flag
  local sub_graph_v = self.sub_graph_v
  local sub_graph_edgeidx = self.sub_graph_edgeidx
  
  local searched_cnt = {0}
  while true do
    local invsrc = sub_graph_v[self.sub_graph_size]
    if invsrc == self.spos then break end
    local eddsrc = self.edge_dst[invsrc]
    local eddiisrc = self.edge_dst_invedge_idx[invsrc]
    local i = searched_cnt[self.sub_graph_size]
    if i < #eddsrc then
      i = i + 1
      searched_cnt[self.sub_graph_size] = i
      local invdst = eddsrc[i]
      local j = eddiisrc[i]
      if 0 < edge_cap[invdst][j]
      and len[invdst] + edge_cost[invdst][j] == len[invsrc]
      and not sub_graph_flag[invdst] then
        self.sub_graph_size = self.sub_graph_size + 1
        sub_graph_v[self.sub_graph_size] = invdst
        sub_graph_edgeidx[self.sub_graph_size - 1] = j
        sub_graph_flag[invdst] = true
        searched_cnt[self.sub_graph_size] = 0
      end
    else
      self.sub_graph_size = self.sub_graph_size - 1
    end
  end
end

MinCostFlow.walkDK = function(self)
  -- Dijkstra-like walk
  local edge_dst, edge_cost, edge_cap = self.edge_dst, self.edge_cost, self.edge_cap
  local len = self.len
  local tasklim = self.n
  if not self.taskstate then
    self.taskstate = {}
    self.tasks = {}
    for i = 1, tasklim do self.taskstate[i] = false end
  end
  local taskstate = self.taskstate
  local tasks = self.tasks
  local tasknum, done = 0, 0
  local function addtask(idx)
    if not taskstate[idx] and idx ~= self.tpos then
      taskstate[idx] = true
      tasknum = tasknum + 1
      local taskidx = tasknum % tasklim
      if taskidx == 0 then taskidx = tasklim end
      tasks[taskidx] = idx
    end
  end
  local function walk(src, dst, cost)
    if len[src] + cost < len[dst] then
      len[dst] = len[src] + cost
      addtask(dst)
    end
  end
  addtask(self.spos)
  while done < tasknum do
    done = done + 1
    local taskidx = done % tasklim
    if taskidx == 0 then taskidx = tasklim end
    local idx = tasks[taskidx]
    taskstate[idx] = false
    local eddst, edcap, edcost = edge_dst[idx], edge_cap[idx], edge_cost[idx]
    for i = 1, #eddst do
      if 0 < edcap[i] then
        walk(idx, eddst[i], edcost[i])
      end
    end
  end
end
MinCostFlow.walkBF = function(self)
  --Bellman-Ford walk
  local edge_dst, edge_cost, edge_cap = self.edge_dst, self.edge_cost, self.edge_cap
  local len = self.len
  local n = self.n
  if not self.updated1 then
    self.updated1 = {}
    self.updated2 = {}
  end
  local updated1, updated2 = self.updated1, self.updated2
  for i = 1, n do
    updated1[i] = false
  end
  updated1[self.spos] = true
  len[self.spos] = 0
  for irp = 1, n do
    local updsrc = irp % 2 == 1 and updated1 or updated2
    local upddst = irp % 2 == 1 and updated2 or updated1
    for i = 1, n do
      upddst[i] = false
    end
    local alldone = true
    for src = 1, n do
      if updsrc[src] then
        local eddst, edcap, edcost = edge_dst[src], edge_cap[src], edge_cost[src]
        for i = 1, #eddst do
          if 0 < edcap[i] then
            local dst = eddst[i]
            local cost = edcost[i]
            if len[src] + cost < len[dst] then
              len[dst] = len[src] + cost
              upddst[dst] = true
              alldone = false
            end
          end
        end
      end
    end
    if alldone then break end
  end
end

MinCostFlow.makeSubGraph = function(self)
  local inf = self.inf
  local len = self.len
  local n = self.n
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local edge_cost = self.edge_cost
  local sub_graph_v = self.sub_graph_v
  local sub_graph_edgeidx = self.sub_graph_edgeidx
  local sub_graph_flag = self.sub_graph_flag
  for i = 1, n do
    len[i] = inf
    sub_graph_flag[i] = false
  end
  len[self.spos] = 0

  -- Bellman-Ford is good, but Dijkstra-like is very fast in some case
  self:walkDK()
  -- self:walkBF()

  self.sub_graph_size = 0
  if inf <= len[self.tpos] then
    return 0
  end
  -- restore route (from tpos to spos)
  self.sub_graph_size = 1
  sub_graph_v[1] = self.tpos
  self:invwalk_recursive(self.tpos)
  local min_capacity = inf
  for i = self.sub_graph_size, 2, -1 do
    local src = sub_graph_v[i]
    local j = sub_graph_edgeidx[i - 1]
    min_capacity = mmi(min_capacity, edge_cap[src][j])
  end
  return min_capacity
end

MinCostFlow.flow = function(self, capacity)
  local edge_dst, edge_cap = self.edge_dst, self.edge_cap
  local edge_dst_invedge_idx = self.edge_dst_invedge_idx
  local sub_graph_v = self.sub_graph_v
  local sub_graph_edgeidx = self.sub_graph_edgeidx
  for i = self.sub_graph_size, 2, -1 do
    local src = sub_graph_v[i]
    local dst = sub_graph_v[i - 1]
    local j = sub_graph_edgeidx[i - 1]
    edge_cap[src][j] = edge_cap[src][j] - capacity
    local k = edge_dst_invedge_idx[src][j]
    edge_cap[dst][k] = edge_cap[dst][k] + capacity
  end
end

MinCostFlow.getMinCostFlow = function(self, amount, invalid)
  local ret = 0
  local cap = self:makeSubGraph()
  while 0 < cap do
    cap = mmi(amount, cap)
    ret = ret + self.len[self.tpos] * cap
    self:flow(cap)
    amount = amount - cap
    if 0 < amount then
      cap = self:makeSubGraph()
    else
      break
    end
  end
  if 0 < amount then return invalid end
  return ret
end

local mcf = MinCostFlow
local n = io.read("*n")
local m = io.read("*n")
mcf:initialize(n, 1, n, 1000000007 * 100000)
for i = 1, m do
  local u, v, c, d = io.read("*n", "*n", "*n", "*n")
  mcf:addEdge(u, v, c, 1)
  mcf:addEdge(v, u, c, 1)
  mcf:addEdge(u, v, d, 1)
  mcf:addEdge(v, u, d, 1)
end
print(mcf:getMinCostFlow(2))

0