結果
問題 | No.798 コレクション |
ユーザー | persimmon-persimmon |
提出日時 | 2021-06-30 16:37:10 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,970 bytes |
コンパイル時間 | 366 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 408,192 KB |
最終ジャッジ日時 | 2024-06-27 02:49:03 |
合計ジャッジ時間 | 4,618 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
58,368 KB |
testcase_01 | AC | 41 ms
52,992 KB |
testcase_02 | AC | 43 ms
53,248 KB |
testcase_03 | AC | 40 ms
52,608 KB |
testcase_04 | AC | 41 ms
53,120 KB |
testcase_05 | AC | 42 ms
52,992 KB |
testcase_06 | AC | 39 ms
53,376 KB |
testcase_07 | AC | 41 ms
53,376 KB |
testcase_08 | AC | 41 ms
53,084 KB |
testcase_09 | AC | 47 ms
59,008 KB |
testcase_10 | AC | 46 ms
59,264 KB |
testcase_11 | AC | 41 ms
52,992 KB |
testcase_12 | AC | 47 ms
53,248 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 | -- | - |
ソースコード
# 最小費用流 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)