結果

問題 No.1288 yuki collection
ユーザー ああいいああいい
提出日時 2022-04-13 23:33:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,891 bytes
コンパイル時間 318 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 94,664 KB
最終ジャッジ日時 2024-06-06 09:51:50
合計ジャッジ時間 24,608 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,992 KB
testcase_01 AC 38 ms
52,992 KB
testcase_02 AC 38 ms
53,248 KB
testcase_03 AC 69 ms
72,448 KB
testcase_04 AC 38 ms
52,992 KB
testcase_05 AC 39 ms
52,992 KB
testcase_06 AC 47 ms
61,440 KB
testcase_07 AC 37 ms
53,376 KB
testcase_08 AC 89 ms
75,892 KB
testcase_09 AC 82 ms
76,416 KB
testcase_10 AC 88 ms
76,032 KB
testcase_11 AC 66 ms
69,888 KB
testcase_12 AC 81 ms
76,272 KB
testcase_13 AC 1,105 ms
93,416 KB
testcase_14 AC 1,164 ms
93,036 KB
testcase_15 AC 931 ms
89,752 KB
testcase_16 AC 946 ms
89,700 KB
testcase_17 AC 1,105 ms
93,000 KB
testcase_18 WA -
testcase_19 AC 1,132 ms
94,664 KB
testcase_20 AC 1,117 ms
93,976 KB
testcase_21 AC 914 ms
93,540 KB
testcase_22 AC 923 ms
93,020 KB
testcase_23 AC 926 ms
92,728 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 518 ms
85,384 KB
testcase_28 AC 651 ms
87,428 KB
testcase_29 WA -
testcase_30 AC 155 ms
79,044 KB
testcase_31 AC 171 ms
79,688 KB
testcase_32 AC 166 ms
79,064 KB
testcase_33 AC 844 ms
92,644 KB
testcase_34 AC 865 ms
87,820 KB
testcase_35 AC 810 ms
88,156 KB
testcase_36 AC 824 ms
88,944 KB
testcase_37 AC 818 ms
89,288 KB
testcase_38 AC 313 ms
80,952 KB
testcase_39 AC 312 ms
81,004 KB
testcase_40 AC 97 ms
77,752 KB
testcase_41 AC 38 ms
52,736 KB
testcase_42 AC 37 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 ** 10
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)
0