結果

問題 No.200 カードファイト!
ユーザー ああいいああいい
提出日時 2022-04-14 09:43:48
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,640 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 82,412 KB
最終ジャッジ日時 2024-06-06 13:45:18
合計ジャッジ時間 4,915 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 168 ms
78,844 KB
testcase_01 WA -
testcase_02 AC 41 ms
53,248 KB
testcase_03 AC 57 ms
62,208 KB
testcase_04 AC 54 ms
61,312 KB
testcase_05 AC 191 ms
79,424 KB
testcase_06 AC 185 ms
79,076 KB
testcase_07 AC 173 ms
79,376 KB
testcase_08 AC 156 ms
78,476 KB
testcase_09 AC 196 ms
79,628 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 49 ms
60,032 KB
testcase_14 AC 46 ms
59,136 KB
testcase_15 AC 45 ms
58,624 KB
testcase_16 AC 39 ms
53,376 KB
testcase_17 AC 52 ms
61,184 KB
testcase_18 AC 53 ms
61,824 KB
testcase_19 AC 98 ms
76,416 KB
testcase_20 AC 187 ms
79,388 KB
testcase_21 AC 90 ms
76,288 KB
testcase_22 AC 95 ms
76,544 KB
testcase_23 AC 59 ms
64,256 KB
testcase_24 AC 104 ms
76,800 KB
testcase_25 AC 165 ms
77,908 KB
testcase_26 AC 49 ms
59,648 KB
testcase_27 AC 115 ms
76,764 KB
testcase_28 AC 211 ms
81,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#その3 ベルマンフォード+ダイクストラ
from heapq import heappop,heappush
class MinCostFlow:
    inf = 10 ** 18

    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.H = [0] * N
        self.first = True
        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)
        return len(self.edge) - 1
    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
        prev_v = [0] * N
        prev_e = [None] * N

        d0 = [inf] * N
        dist = [inf] * N

        if self.first:
            self.first = False
            dist[:] = d0
            dist[s] = 0
            update = 1
            while update:
                update = 0
                for v in range(N):
                    if dist[v] == inf:continue
                    for e in G[v]:
                        w,cap,cost,_ = e
                        if cap > 0 and dist[v] + cost < dist[w]:
                            dist[w] = dist[v] + cost
                            prev_v[w] = v
                            prev_e[w] = e
                            update = 1
            H[:] = dist
            if dist[t] == inf:
                return None
            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]
        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())
na = int(input())
A = list(map(int,input().split()))
nb = int(input())
B = list(map(int,input().split()))
l = A.copy()
while len(l) < N:
    l += A
s = B.copy()
while len(s) < N:
    s += B
n = len(l)
m = len(s)
mincost = MinCostFlow(n * m + 2)
start = 0
end = n * m + 1
inf = 1000
for i in range(n):
    qi = i // na
    for j in  range(m):
        qj = j // nb
        q = min(qi,qj) * inf + qi + qj + 5
        if l[i] > s[j]:
            mincost.add_edge(i+1,j + 1 + n,1,-1 + q * inf)
        else:
            mincost.add_edge(i+1,j + 1 + n,1,0 + q * inf)
inf = 1000
for i in range(n):
    mincost.add_edge(start,i + 1,1,0)
for j in range(m):
    mincost.add_edge(j + 1 + n,end,1,0)
ans = -mincost.flow(start,end,N)
print(ans%inf)
0