結果

問題 No.1288 yuki collection
ユーザー ああいいああいい
提出日時 2022-04-13 23:38:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,891 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 97,036 KB
最終ジャッジ日時 2023-08-25 15:36:04
合計ジャッジ時間 27,596 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,764 KB
testcase_01 AC 75 ms
70,980 KB
testcase_02 AC 75 ms
70,820 KB
testcase_03 AC 112 ms
77,008 KB
testcase_04 AC 74 ms
71,156 KB
testcase_05 AC 76 ms
70,840 KB
testcase_06 AC 85 ms
75,948 KB
testcase_07 AC 74 ms
70,988 KB
testcase_08 AC 124 ms
77,176 KB
testcase_09 AC 115 ms
77,376 KB
testcase_10 AC 124 ms
77,244 KB
testcase_11 AC 105 ms
76,936 KB
testcase_12 AC 115 ms
77,124 KB
testcase_13 AC 1,215 ms
95,416 KB
testcase_14 AC 1,228 ms
94,424 KB
testcase_15 AC 1,027 ms
90,964 KB
testcase_16 AC 1,017 ms
90,708 KB
testcase_17 AC 1,218 ms
94,352 KB
testcase_18 WA -
testcase_19 AC 1,283 ms
95,816 KB
testcase_20 AC 1,226 ms
95,904 KB
testcase_21 AC 982 ms
94,772 KB
testcase_22 AC 991 ms
94,640 KB
testcase_23 AC 996 ms
94,896 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 593 ms
88,288 KB
testcase_28 AC 663 ms
89,096 KB
testcase_29 WA -
testcase_30 AC 200 ms
80,512 KB
testcase_31 AC 212 ms
80,956 KB
testcase_32 AC 212 ms
81,296 KB
testcase_33 AC 901 ms
94,308 KB
testcase_34 AC 920 ms
90,408 KB
testcase_35 AC 925 ms
89,860 KB
testcase_36 AC 911 ms
90,008 KB
testcase_37 AC 941 ms
90,124 KB
testcase_38 AC 358 ms
82,688 KB
testcase_39 AC 366 ms
82,788 KB
testcase_40 AC 134 ms
79,312 KB
testcase_41 AC 74 ms
71,044 KB
testcase_42 AC 74 ms
70,920 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
    #print(tmp)
    ans += tmp
print(ans)
#print(dat)
0