結果
問題 | No.1678 Coin Trade (Multiple) |
ユーザー | chocorusk |
提出日時 | 2021-08-02 03:30:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 4,829 ms / 5,000 ms |
コード長 | 2,958 bytes |
コンパイル時間 | 172 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 196,116 KB |
最終ジャッジ日時 | 2024-06-11 21:38:19 |
合計ジャッジ時間 | 71,473 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
53,012 KB |
testcase_01 | AC | 39 ms
52,864 KB |
testcase_02 | AC | 39 ms
53,376 KB |
testcase_03 | AC | 487 ms
98,568 KB |
testcase_04 | AC | 2,318 ms
142,900 KB |
testcase_05 | AC | 1,016 ms
118,608 KB |
testcase_06 | AC | 723 ms
112,004 KB |
testcase_07 | AC | 2,154 ms
133,248 KB |
testcase_08 | AC | 1,349 ms
119,600 KB |
testcase_09 | AC | 1,037 ms
119,060 KB |
testcase_10 | AC | 480 ms
89,760 KB |
testcase_11 | AC | 1,658 ms
129,424 KB |
testcase_12 | AC | 432 ms
91,500 KB |
testcase_13 | AC | 3,593 ms
171,124 KB |
testcase_14 | AC | 1,166 ms
112,772 KB |
testcase_15 | AC | 1,232 ms
119,336 KB |
testcase_16 | AC | 369 ms
100,948 KB |
testcase_17 | AC | 1,062 ms
119,208 KB |
testcase_18 | AC | 61 ms
67,328 KB |
testcase_19 | AC | 40 ms
53,376 KB |
testcase_20 | AC | 47 ms
60,672 KB |
testcase_21 | AC | 51 ms
62,080 KB |
testcase_22 | AC | 57 ms
65,280 KB |
testcase_23 | AC | 45 ms
59,136 KB |
testcase_24 | AC | 60 ms
66,176 KB |
testcase_25 | AC | 40 ms
54,016 KB |
testcase_26 | AC | 58 ms
65,536 KB |
testcase_27 | AC | 48 ms
60,544 KB |
testcase_28 | AC | 50 ms
62,464 KB |
testcase_29 | AC | 41 ms
53,760 KB |
testcase_30 | AC | 47 ms
60,032 KB |
testcase_31 | AC | 44 ms
59,136 KB |
testcase_32 | AC | 41 ms
54,016 KB |
testcase_33 | AC | 62 ms
67,860 KB |
testcase_34 | AC | 52 ms
63,744 KB |
testcase_35 | AC | 40 ms
54,016 KB |
testcase_36 | AC | 64 ms
68,096 KB |
testcase_37 | AC | 62 ms
67,840 KB |
testcase_38 | AC | 49 ms
61,568 KB |
testcase_39 | AC | 48 ms
61,440 KB |
testcase_40 | AC | 58 ms
65,664 KB |
testcase_41 | AC | 54 ms
63,872 KB |
testcase_42 | AC | 65 ms
68,608 KB |
testcase_43 | AC | 68 ms
70,016 KB |
testcase_44 | AC | 47 ms
61,184 KB |
testcase_45 | AC | 40 ms
53,632 KB |
testcase_46 | AC | 69 ms
70,016 KB |
testcase_47 | AC | 55 ms
64,332 KB |
testcase_48 | AC | 4,560 ms
194,044 KB |
testcase_49 | AC | 4,716 ms
194,508 KB |
testcase_50 | AC | 4,574 ms
194,004 KB |
testcase_51 | AC | 4,800 ms
191,964 KB |
testcase_52 | AC | 4,599 ms
192,988 KB |
testcase_53 | AC | 4,706 ms
193,612 KB |
testcase_54 | AC | 4,761 ms
196,116 KB |
testcase_55 | AC | 4,787 ms
192,888 KB |
testcase_56 | AC | 4,829 ms
191,708 KB |
testcase_57 | AC | 4,491 ms
193,448 KB |
testcase_58 | AC | 1,947 ms
131,524 KB |
ソースコード
import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines import heapq class MinCostFlow: def __init__(self, n, neg=False): self.n=n self.g=[[] for _ in range(n)] self.h=[0]*n self.neg=neg def add_edge(self, frm, to, cap, cost): g=self.g g[frm].append([to, cap, cost, len(g[to])]) g[to].append([frm, 0, -cost, len(g[frm])-1]) def bellman_ford(self, s, prevv, preve): n=self.n g=self.g h=self.h INF=10**18 for x in range(n): h[x]=INF h[s]=0 for _ in range(n-1): for x in range(n): for i, e in enumerate(g[x]): if e[1]==0: continue to, cost=e[0], e[2] if h[to]>h[x]+cost: h[to]=h[x]+cost prevv[to]=x preve[to]=i def flow(self, s, t, f): res=0 n=self.n g=self.g h=self.h INF=10**16 LOG=16 #n<2**LOG prevv=[0]*n preve=[0]*n while f>0: if self.neg: self.neg=False self.bellman_ford(s, prevv, preve) if h[t]==INF: return -1 else: que=[] dist=[INF]*n dist[s]=0 heapq.heappush(que, s) while que: p=heapq.heappop(que) v=(p&((1<<LOG)-1)) if dist[v]<(p>>LOG): continue for i, e in enumerate(g[v]): if e[1]==0: continue to, cost=e[0], e[2] if dist[to]>dist[v]+cost+h[v]-h[to]: dist[to]=dist[v]+cost+h[v]-h[to] prevv[to]=v preve[to]=i heapq.heappush(que, (dist[to]<<LOG)^to) for i, d in enumerate(dist): h[i]+=d if dist[t]==INF: return -1 d=f v=t while v!=s: d=min(d, g[prevv[v]][preve[v]][1]) v=prevv[v] f-=d res+=d*h[t] v=t while v!=s: g[prevv[v]][preve[v]][1]-=d rev=g[prevv[v]][preve[v]][3] g[v][rev][1]+=d v=prevv[v] return res n, k=map(int, readline().split()) inf=10**9 g=MinCostFlow(n) a=[0]*n for i in range(n): a[i], m=map(int, readline().split()) b=list(map(int, readline().split())) for bi in b: bi-=1 g.add_edge(bi, i, 1, a[bi]-a[i]+inf*(i-bi)) for i in range(n-1): g.add_edge(i, i+1, k, inf) print(inf*(n-1)*k-g.flow(0, n-1, k))