結果

問題 No.1288 yuki collection
ユーザー ああいいああいい
提出日時 2022-04-13 23:45:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,376 ms / 5,000 ms
コード長 2,912 bytes
コンパイル時間 740 ms
コンパイル使用メモリ 86,636 KB
実行使用メモリ 96,100 KB
最終ジャッジ日時 2023-08-25 15:45:32
合計ジャッジ時間 30,505 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,232 KB
testcase_01 AC 76 ms
71,000 KB
testcase_02 AC 74 ms
70,976 KB
testcase_03 AC 119 ms
77,156 KB
testcase_04 AC 75 ms
71,068 KB
testcase_05 AC 77 ms
71,232 KB
testcase_06 AC 84 ms
75,836 KB
testcase_07 AC 76 ms
70,876 KB
testcase_08 AC 126 ms
77,280 KB
testcase_09 AC 117 ms
77,504 KB
testcase_10 AC 128 ms
77,360 KB
testcase_11 AC 108 ms
77,212 KB
testcase_12 AC 118 ms
77,260 KB
testcase_13 AC 1,333 ms
95,704 KB
testcase_14 AC 1,292 ms
94,452 KB
testcase_15 AC 1,059 ms
90,996 KB
testcase_16 AC 1,068 ms
90,888 KB
testcase_17 AC 1,264 ms
94,480 KB
testcase_18 AC 1,332 ms
95,868 KB
testcase_19 AC 1,350 ms
95,804 KB
testcase_20 AC 1,316 ms
96,100 KB
testcase_21 AC 1,094 ms
94,452 KB
testcase_22 AC 1,099 ms
94,496 KB
testcase_23 AC 1,100 ms
94,780 KB
testcase_24 AC 1,376 ms
96,036 KB
testcase_25 AC 1,311 ms
95,324 KB
testcase_26 AC 1,306 ms
95,308 KB
testcase_27 AC 639 ms
88,388 KB
testcase_28 AC 751 ms
89,188 KB
testcase_29 AC 864 ms
89,248 KB
testcase_30 AC 208 ms
80,848 KB
testcase_31 AC 223 ms
81,196 KB
testcase_32 AC 215 ms
81,092 KB
testcase_33 AC 986 ms
94,224 KB
testcase_34 AC 1,003 ms
90,216 KB
testcase_35 AC 948 ms
89,824 KB
testcase_36 AC 973 ms
90,244 KB
testcase_37 AC 989 ms
90,220 KB
testcase_38 AC 382 ms
82,900 KB
testcase_39 AC 390 ms
82,784 KB
testcase_40 AC 140 ms
79,412 KB
testcase_41 AC 77 ms
71,132 KB
testcase_42 AC 77 ms
71,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#その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
    if tmp < 0:break
    #print(tmp)
    ans += tmp
print(ans)
#print(dat)
0