結果

問題 No.1301 Strange Graph Shortest Path
ユーザー wolgnikwolgnik
提出日時 2020-11-27 23:00:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,424 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 311,744 KB
最終ジャッジ日時 2023-10-09 21:57:44
合計ジャッジ時間 62,654 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,244 KB
testcase_01 AC 81 ms
71,492 KB
testcase_02 AC 2,041 ms
311,304 KB
testcase_03 AC 1,821 ms
276,224 KB
testcase_04 AC 1,980 ms
297,432 KB
testcase_05 AC 1,873 ms
305,044 KB
testcase_06 AC 1,856 ms
305,380 KB
testcase_07 AC 2,013 ms
308,448 KB
testcase_08 AC 1,790 ms
278,588 KB
testcase_09 AC 1,745 ms
294,812 KB
testcase_10 AC 1,640 ms
277,244 KB
testcase_11 AC 1,720 ms
311,744 KB
testcase_12 AC 1,921 ms
303,196 KB
testcase_13 AC 1,903 ms
303,896 KB
testcase_14 AC 2,170 ms
295,140 KB
testcase_15 AC 1,779 ms
292,188 KB
testcase_16 AC 1,931 ms
303,980 KB
testcase_17 AC 1,866 ms
310,636 KB
testcase_18 AC 1,896 ms
294,644 KB
testcase_19 AC 1,834 ms
304,304 KB
testcase_20 AC 1,709 ms
301,916 KB
testcase_21 AC 1,969 ms
299,604 KB
testcase_22 AC 1,828 ms
300,016 KB
testcase_23 AC 1,894 ms
306,244 KB
testcase_24 AC 1,856 ms
297,988 KB
testcase_25 AC 2,097 ms
297,720 KB
testcase_26 AC 2,035 ms
296,660 KB
testcase_27 AC 1,796 ms
303,008 KB
testcase_28 AC 1,829 ms
294,296 KB
testcase_29 WA -
testcase_30 AC 1,883 ms
309,680 KB
testcase_31 AC 2,095 ms
295,140 KB
testcase_32 AC 80 ms
71,320 KB
testcase_33 AC 1,005 ms
306,840 KB
testcase_34 AC 1,884 ms
302,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N, M = map(int, input().split())

from heapq import heappop, heappush, heapify
class MinCostFlow():
  def __init__(self, n):
    self.n = n
    self.graph = [[] for _ in range(n)]
    self.pos = []
  def add_edge(self, fr, to, cap, cost):
    m = len(self.pos)
    self.pos.append((fr, len(self.graph[fr])))
    self.graph[fr].append([to, len(self.graph[to]), cap, cost])
    self.graph[to].append([fr, len(self.graph[fr]) - 1, 0, -cost])
    return m
  def get_edge(self, idx):
    to, rev, cap, cost = self.graph[self.pos[idx][0]][self.pos[idx][1]]
    rev_to, rev_rev, rev_cap, rev_cost = self.graph[to][rev]
    return self.pos[idx][0], to, cap + rev_cap, rev_cap, cost
  def edges(self):
    for i in range(len(self.pos)):
      yield self.get_edge(i)
  def dual_ref(self, s, t):
    dist = [2**63 - 1] * self.n
    dist[s] = 0
    vis = [0] * self.n
    self.pv = [-1] * self.n
    self.pe = [-1] * self.n
    queue = []
    heappush(queue, (0, s))
    while queue:
      k, v = heappop(queue)
      if vis[v]: continue
      vis[v] = True
      if v == t: break
      for i in range(len(self.graph[v])):
        to, rev, cap, cost = self.graph[v][i]
        if vis[to] or cap == 0: continue
        cost += self.dual[v] - self.dual[to]
        if dist[to] - dist[v] > cost:
          dist[to] = dist[v] + cost
          self.pv[to] = v
          self.pe[to] = i
          heappush(queue, (dist[to], to))
    if not vis[t]: return False
    for v in range(self.n):
      if not vis[v]: continue
      self.dual[v] -= dist[t] - dist[v]
    return True
  def flow(self, s, t): return self.flow_with_limit(s, t, 2**63 - 1)
  def flow_with_limit(self, s, t, limit): return self.slope_with_limit(s, t, limit)[-1]
  def slope(self, s, t): return self.slope_with_limit(s, t, 2**63 - 1)
  def slope_with_limit(self, s, t, limit):
    flow = 0
    cost = 0
    prev_cost = -1
    res = [(flow, cost)]
    self.dual = [0] * self.n
    while flow < limit:
      if not self.dual_ref(s, t): break
      c = limit - flow
      v = t
      while v != s:
        c = min(c, self.graph[self.pv[v]][self.pe[v]][2])
        v = self.pv[v]
      v = t
      while v != s:
        to, rev, cap, _ = self.graph[self.pv[v]][self.pe[v]]
        self.graph[self.pv[v]][self.pe[v]][2] -= c
        self.graph[v][rev][2] += c
        v = self.pv[v]
      d = -self.dual[s]
      flow += c
      cost += c * d
      if prev_cost == d: res.pop()
      res.append((flow, cost))
      prev_cost = cost
    return res

mcf = MinCostFlow(N)
edges = []
for _ in range(M):
  u, v, c, d = map(int, input().split())
  edges.append((u, v, c, d))
  mcf.add_edge(u - 1, v - 1, 1, c)
  mcf.add_edge(v - 1, u - 1, 1, c)
mcf2 = mcf.flow_with_limit(0, N - 1, 2)
mcf = MinCostFlow(N)
for u, v, c, d in edges:
  mcf.add_edge(u - 1, v - 1, 1, c)
  mcf.add_edge(v - 1, u - 1, 1, c)
mcf1 = mcf.flow_with_limit(0, N - 1, 1)
if mcf2[0] == 2 and mcf1[1] * 2 == mcf2[1]:
  print(mcf2[1])
  exit(0)
mcfedges = list(mcf.edges())
mcf = MinCostFlow(N)
for i in range(M):
  x = i * 2
  fr, to, cap, flow, cost = mcfedges[x]
  flow += mcfedges[x + 1][3]
  if flow: 
    mcf.add_edge(fr, to, cap, edges[i][-1])
    mcf.add_edge(to, fr, cap, edges[i][-1])
  else: 
    mcf.add_edge(fr, to, cap, cost)
    mcf.add_edge(to, fr, cap, cost)
res = mcf1[1] + mcf.flow_with_limit(0, N - 1, 1)[1]
if mcf2[0] == 2: res = min(res, mcf2[1])
print(res)
0