結果

問題 No.1324 Approximate the Matrix
ユーザー ああいいああいい
提出日時 2022-04-14 11:12:10
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,595 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 128,328 KB
最終ジャッジ日時 2023-08-25 19:59:32
合計ジャッジ時間 5,686 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
75,788 KB
testcase_01 AC 74 ms
71,284 KB
testcase_02 AC 72 ms
71,288 KB
testcase_03 AC 448 ms
85,304 KB
testcase_04 AC 526 ms
85,048 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

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
                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]
            """
            H = [h + d if d < inf else h 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,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
P = [list(map(int,input().split())) for _ in range(N)]

mincost = MinCostFlow(N + N + 2)
s = N + N
t = N + N + 1
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,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)
print(ans)
0