結果

問題 No.1288 yuki collection
ユーザー ああいいああいい
提出日時 2022-04-13 23:45:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,343 ms / 5,000 ms
コード長 2,912 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 94,624 KB
最終ジャッジ日時 2024-06-06 10:04:23
合計ジャッジ時間 28,352 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,120 KB
testcase_01 AC 40 ms
52,992 KB
testcase_02 AC 40 ms
53,120 KB
testcase_03 AC 74 ms
72,320 KB
testcase_04 AC 39 ms
53,120 KB
testcase_05 AC 40 ms
52,992 KB
testcase_06 AC 50 ms
61,952 KB
testcase_07 AC 40 ms
53,120 KB
testcase_08 AC 95 ms
75,928 KB
testcase_09 AC 86 ms
76,404 KB
testcase_10 AC 93 ms
76,416 KB
testcase_11 AC 69 ms
70,144 KB
testcase_12 AC 84 ms
76,160 KB
testcase_13 AC 1,319 ms
93,480 KB
testcase_14 AC 1,237 ms
93,712 KB
testcase_15 AC 1,018 ms
89,356 KB
testcase_16 AC 1,067 ms
90,204 KB
testcase_17 AC 1,243 ms
93,220 KB
testcase_18 AC 1,338 ms
94,164 KB
testcase_19 AC 1,328 ms
94,232 KB
testcase_20 AC 1,290 ms
94,364 KB
testcase_21 AC 1,076 ms
93,160 KB
testcase_22 AC 1,074 ms
93,060 KB
testcase_23 AC 1,070 ms
92,596 KB
testcase_24 AC 1,343 ms
94,624 KB
testcase_25 AC 1,302 ms
94,032 KB
testcase_26 AC 1,256 ms
94,092 KB
testcase_27 AC 604 ms
85,516 KB
testcase_28 AC 718 ms
86,908 KB
testcase_29 AC 810 ms
87,396 KB
testcase_30 AC 176 ms
79,272 KB
testcase_31 AC 189 ms
79,152 KB
testcase_32 AC 184 ms
79,272 KB
testcase_33 AC 957 ms
93,028 KB
testcase_34 AC 956 ms
88,108 KB
testcase_35 AC 919 ms
88,400 KB
testcase_36 AC 936 ms
88,800 KB
testcase_37 AC 947 ms
88,820 KB
testcase_38 AC 347 ms
81,316 KB
testcase_39 AC 350 ms
81,000 KB
testcase_40 AC 107 ms
78,272 KB
testcase_41 AC 39 ms
52,992 KB
testcase_42 AC 40 ms
52,992 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