結果

問題 No.1324 Approximate the Matrix
ユーザー ああいいああいい
提出日時 2022-04-14 10:48:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,704 bytes
コンパイル時間 1,007 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 91,304 KB
最終ジャッジ日時 2023-08-25 19:54:01
合計ジャッジ時間 15,806 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,384 KB
testcase_01 AC 72 ms
71,388 KB
testcase_02 AC 74 ms
71,168 KB
testcase_03 AC 540 ms
85,428 KB
testcase_04 AC 531 ms
85,324 KB
testcase_05 AC 544 ms
85,656 KB
testcase_06 AC 540 ms
85,532 KB
testcase_07 AC 570 ms
85,612 KB
testcase_08 AC 160 ms
79,148 KB
testcase_09 AC 170 ms
78,968 KB
testcase_10 AC 186 ms
79,588 KB
testcase_11 AC 235 ms
81,168 KB
testcase_12 AC 181 ms
79,772 KB
testcase_13 AC 153 ms
78,660 KB
testcase_14 AC 235 ms
81,088 KB
testcase_15 AC 177 ms
79,160 KB
testcase_16 AC 138 ms
77,680 KB
testcase_17 AC 243 ms
80,980 KB
testcase_18 WA -
testcase_19 AC 174 ms
79,384 KB
testcase_20 AC 142 ms
78,976 KB
testcase_21 AC 151 ms
78,708 KB
testcase_22 AC 93 ms
76,716 KB
testcase_23 AC 229 ms
80,196 KB
testcase_24 AC 304 ms
82,244 KB
testcase_25 AC 255 ms
81,464 KB
testcase_26 AC 233 ms
80,492 KB
testcase_27 AC 191 ms
79,928 KB
testcase_28 AC 77 ms
71,392 KB
testcase_29 AC 85 ms
75,692 KB
testcase_30 AC 86 ms
75,652 KB
testcase_31 AC 92 ms
76,268 KB
testcase_32 AC 75 ms
71,520 KB
testcase_33 AC 77 ms
71,036 KB
testcase_34 AC 76 ms
71,328 KB
testcase_35 AC 78 ms
71,284 KB
testcase_36 AC 76 ms
71,444 KB
testcase_37 AC 1,067 ms
90,800 KB
testcase_38 AC 1,118 ms
91,248 KB
testcase_39 AC 1,106 ms
90,932 KB
testcase_40 AC 977 ms
91,076 KB
testcase_41 AC 936 ms
91,304 KB
testcase_42 AC 89 ms
76,540 KB
testcase_43 AC 90 ms
76,616 KB
testcase_44 AC 89 ms
76,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#その2 最初はコストが全部正の場合に使えるダイクストラ
from heapq import heappush,heappop
class MinCostFlow:
    inf = 10 ** 10

    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
                if v == t:break
                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]
            """
            for i in range(N):
                if dist[i] < inf:
                    H[i] += dist[i]
            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
import sys
rr = sys.stdin
N,K = map(int,rr.readline().split())
A = list(map(int,rr.readline().split()))
B = list(map(int,rr.readline().split()))
P = [list(map(int,rr.readline().split())) for _ in range(N)]

mincost = MinCostFlow(N + N + 2)
s = N + N
t = N + N + 1
inf = 400
for i in range(N):
    for j in range(N):
        v = N + j
        for k in range(1,min(A[i],B[j])+1):
            mincost.add_edge(i,v,1,inf + (2 * (k - 1) + 1 - 2 * P[i][j]))
for i in range(N):
    mincost.add_edge(s,i,A[i],0)
for j in range(N):
    mincost.add_edge(j + N,t,B[j],0)
ans = 0
for i in range(N):
    for j in range(N):
        ans += P[i][j] ** 2
ans += mincost.flow(s,t,K) - inf * K
print(ans)
0