結果
問題 | No.1288 yuki collection |
ユーザー | ああいい |
提出日時 | 2022-04-13 22:14:57 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,690 bytes |
コンパイル時間 | 174 ms |
コンパイル使用メモリ | 81,920 KB |
実行使用メモリ | 200,644 KB |
最終ジャッジ日時 | 2024-06-06 08:33:47 |
合計ジャッジ時間 | 8,166 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
53,248 KB |
testcase_01 | AC | 40 ms
52,736 KB |
testcase_02 | AC | 43 ms
53,120 KB |
testcase_03 | AC | 113 ms
76,948 KB |
testcase_04 | AC | 42 ms
53,376 KB |
testcase_05 | AC | 41 ms
52,736 KB |
testcase_06 | AC | 64 ms
66,304 KB |
testcase_07 | AC | 40 ms
53,248 KB |
testcase_08 | AC | 121 ms
76,820 KB |
testcase_09 | AC | 110 ms
76,160 KB |
testcase_10 | AC | 136 ms
77,688 KB |
testcase_11 | AC | 99 ms
76,416 KB |
testcase_12 | AC | 123 ms
76,836 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 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
ソースコード
#その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 ** 10 mincost = MinCostFlow(N+2 + N) base = N + 2 for i in range(3): a = dat[i] b = dat[i+1] for x in a: for y in b: if y < x:continue mincost.add_edge(x+base,y+1,1,0) #print(x+1,y+1) for x in dat[-1]: mincost.add_edge(x+base,t,1,0) for x in dat[0]: mincost.add_edge(s,x+1,1,0) for x in range(N): mincost.add_edge(x+1,x + base,1,inf - V[x]) ans = 0 while True: tmp = mincost.flow(s,t,1) if tmp == None:break tmp = inf * 4 - tmp #print(tmp) ans += tmp print(ans) #print(dat)