結果
問題 | No.1288 yuki collection |
ユーザー | ああいい |
提出日時 | 2022-04-13 23:38:09 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,891 bytes |
コンパイル時間 | 188 ms |
コンパイル使用メモリ | 81,780 KB |
実行使用メモリ | 94,988 KB |
最終ジャッジ日時 | 2024-06-06 09:55:59 |
合計ジャッジ時間 | 24,674 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 37 ms
53,120 KB |
testcase_01 | AC | 38 ms
53,120 KB |
testcase_02 | AC | 38 ms
52,736 KB |
testcase_03 | AC | 72 ms
72,704 KB |
testcase_04 | AC | 40 ms
52,992 KB |
testcase_05 | AC | 38 ms
52,992 KB |
testcase_06 | AC | 48 ms
61,440 KB |
testcase_07 | AC | 37 ms
52,992 KB |
testcase_08 | AC | 93 ms
76,288 KB |
testcase_09 | AC | 83 ms
76,032 KB |
testcase_10 | AC | 88 ms
76,196 KB |
testcase_11 | AC | 64 ms
70,732 KB |
testcase_12 | AC | 80 ms
76,392 KB |
testcase_13 | AC | 1,142 ms
93,832 KB |
testcase_14 | AC | 1,174 ms
93,128 KB |
testcase_15 | AC | 921 ms
89,432 KB |
testcase_16 | AC | 934 ms
90,020 KB |
testcase_17 | AC | 1,113 ms
93,336 KB |
testcase_18 | WA | - |
testcase_19 | AC | 1,173 ms
94,732 KB |
testcase_20 | AC | 1,118 ms
94,492 KB |
testcase_21 | AC | 947 ms
93,064 KB |
testcase_22 | AC | 916 ms
92,900 KB |
testcase_23 | AC | 919 ms
92,592 KB |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | AC | 545 ms
85,472 KB |
testcase_28 | AC | 644 ms
87,140 KB |
testcase_29 | WA | - |
testcase_30 | AC | 164 ms
79,092 KB |
testcase_31 | AC | 173 ms
79,396 KB |
testcase_32 | AC | 173 ms
79,660 KB |
testcase_33 | AC | 820 ms
92,852 KB |
testcase_34 | AC | 859 ms
88,208 KB |
testcase_35 | AC | 851 ms
87,944 KB |
testcase_36 | AC | 839 ms
88,444 KB |
testcase_37 | AC | 873 ms
88,936 KB |
testcase_38 | AC | 322 ms
81,000 KB |
testcase_39 | AC | 326 ms
81,000 KB |
testcase_40 | AC | 101 ms
77,696 KB |
testcase_41 | AC | 38 ms
53,120 KB |
testcase_42 | AC | 39 ms
52,864 KB |
ソースコード
#その2 最初はコストが全部正の場合に使えるダイクストラ from heapq import heappush,heappop class MinCostFlow: inf = 10 ** 18 def __init__(self,N): self.N = N self.G = [[] for _ in range(N)] self.H = [0] * N self.edge = [] def add_edge(self,fr,to,cap,cost): e = [to,cap,cost,None] r = e[3] = [fr,0,-cost,e] self.G[fr].append(e) self.G[to].append(r) self.edge.append(e) def get_edge(self,i): return self.edge[i] def flow(self,s,t,f): N = self.N G = self.G inf = MinCostFlow.inf res = 0 H = self.H #ポテンシャル、コストが最初は正なので、最初は0でいい prev_v = [0] * N prev_e = [None] * N d0 = [inf] * N dist = [inf] * N while f: dist[:] = d0 dist[s] = 0 q = [(0,s)] while q: c,v = heappop(q) 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] prev_v[w] = v prev_e[w] = e heappush(q,(r,w)) if dist[t] == inf: return None """ for i in range(N): H[i] += dist[i] """ H = [h + d for h,d in zip(H,dist)] d = f v = t while v != s: d = min(d,prev_e[v][1]) v = prev_v[v] f -= d res += d * H[t] v = t while v != s: e = prev_e[v] e[1] -= d e[3][1] += d v = prev_v[v] return res N = int(input()) S = input() V = list(map(int,input().split())) d = {"y":0,"u":1,"k":2,"i":3} dat = [[] for _ in range(4)] for i in range(N): dat[d[S[i]]].append(i) s = 0 t = N + 1 inf = 10 ** 11 mincost = MinCostFlow(N + 2) """ for x in dat[0]: mincost.add_edge(s,x + 1,inf,0) """ if dat[0]: mincost.add_edge(s,dat[0][0]+1,inf,0) for x in dat[-1]: mincost.add_edge(x + 1,t,1,inf - V[x]) for i in range(3): a = dat[i] b = dat[i+1] right = 0 for left in range(len(a)): while right < len(b) and b[right] < a[left]: right += 1 if right < len(b): mincost.add_edge(a[left] + 1,b[right] + 1,1,inf - V[a[left]]) for i in range(4): a = dat[i] for j in range(len(a) - 1): mincost.add_edge(a[j] + 1,a[j+1] + 1,inf,0) ans = 0 while True: tmp = mincost.flow(s,t,1) if tmp is None: break tmp = 4 * inf - tmp #print(tmp) ans += tmp print(ans) #print(dat)