結果

問題 No.798 コレクション
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-06-30 16:37:10
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,970 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 86,796 KB
実行使用メモリ 75,408 KB
最終ジャッジ日時 2023-09-09 09:39:58
合計ジャッジ時間 6,006 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,552 KB
testcase_01 AC 72 ms
71,432 KB
testcase_02 AC 76 ms
71,108 KB
testcase_03 AC 72 ms
71,232 KB
testcase_04 AC 72 ms
71,188 KB
testcase_05 AC 72 ms
71,492 KB
testcase_06 AC 72 ms
71,232 KB
testcase_07 AC 72 ms
71,356 KB
testcase_08 AC 72 ms
71,300 KB
testcase_09 AC 75 ms
75,408 KB
testcase_10 AC 75 ms
75,304 KB
testcase_11 AC 71 ms
71,420 KB
testcase_12 AC 71 ms
71,240 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# 最小費用流
from heapq import heappush, heappop
class MinCostFlow:
  INF = 10**18

  def __init__(self, N):
    self.N = N
    self.G = [[] for i in range(N)]

  def add_edge(self, fr, to, cap, cost):
    forward = [to, cap, cost, None]
    backward = forward[3] = [fr, 0, -cost, forward]
    self.G[fr].append(forward)
    self.G[to].append(backward)

  def flow(self, s, t, f):
    N = self.N; G = self.G
    INF = MinCostFlow.INF

    res = 0
    H = [0]*N
    prv_v = [0]*N
    prv_e = [None]*N

    d0 = [INF]*N
    dist = [INF]*N

    while f:
      dist[:] = d0
      dist[s] = 0
      que = [(0, s)]

      while que:
        c, v = heappop(que)
        if dist[v] < c:
          continue
        r0 = dist[v] + H[v]
        for e in G[v]:
          w, cap, cost, _ = e
          if cap > 0 and r0 + cost - H[w] < dist[w]:
            dist[w] = r = r0 + cost - H[w]
            prv_v[w] = v; prv_e[w] = e
            heappush(que, (r, w))
      if dist[t] == INF:
        return None

      for i in range(N):
        H[i] += dist[i]

      d = f; v = t
      while v != s:
        d = min(d, prv_e[v][1])
        v = prv_v[v]
      f -= d
      res += d * H[t]
      v = t
      while v != s:
        e = prv_e[v]
        e[1] -= d
        e[3][1] += d
        v = prv_v[v]
    return res
def main1(n,ab):
    k=0
    cnt=0
    for i in range(n):
        cnt+=1
        if i%2==1:
            cnt+=1
        if cnt>=n:
            k=i+1
            break
    # k個の商品を買う
    mcf=MinCostFlow(n+k+2)
    # i:商品i
    # n+j:j日目
    # n+k:start
    # n+k+1:goal
    for i,(a,b) in enumerate(ab):
        mcf.add_edge(n+k,i,1,0)
        for j in range(k):
            mcf.add_edge(i,n+j,1,a+b*j)
    for j in range(k):
        mcf.add_edge(n+j,n+k+1,1,0)
    ans=mcf.flow(n+k,n+k+1,k)
    return ans

if __name__=='__main__':
    n=int(input())
    ab=[list(map(int,input().split())) for _ in range(n)]
    ret1=main1(n,ab)
    print(ret1)
0