結果

問題 No.1324 Approximate the Matrix
ユーザー theory_and_metheory_and_me
提出日時 2020-12-09 02:15:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,009 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 26,240 KB
最終ジャッジ日時 2024-09-19 23:38:40
合計ジャッジ時間 30,066 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,136 KB
testcase_01 AC 33 ms
11,136 KB
testcase_02 AC 32 ms
11,264 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 102 ms
12,800 KB
testcase_09 AC 101 ms
12,288 KB
testcase_10 AC 208 ms
14,208 KB
testcase_11 AC 433 ms
16,512 KB
testcase_12 AC 100 ms
12,160 KB
testcase_13 AC 62 ms
11,904 KB
testcase_14 AC 462 ms
17,024 KB
testcase_15 AC 141 ms
13,568 KB
testcase_16 AC 49 ms
11,520 KB
testcase_17 AC 331 ms
14,336 KB
testcase_18 AC 89 ms
12,416 KB
testcase_19 AC 135 ms
12,032 KB
testcase_20 AC 61 ms
11,904 KB
testcase_21 AC 58 ms
11,520 KB
testcase_22 AC 56 ms
11,648 KB
testcase_23 AC 328 ms
13,440 KB
testcase_24 AC 699 ms
17,792 KB
testcase_25 AC 356 ms
14,848 KB
testcase_26 AC 312 ms
14,464 KB
testcase_27 AC 173 ms
12,672 KB
testcase_28 AC 33 ms
11,264 KB
testcase_29 AC 36 ms
11,264 KB
testcase_30 AC 39 ms
11,264 KB
testcase_31 AC 39 ms
11,136 KB
testcase_32 AC 33 ms
11,136 KB
testcase_33 AC 33 ms
11,264 KB
testcase_34 AC 33 ms
11,136 KB
testcase_35 AC 34 ms
11,136 KB
testcase_36 AC 33 ms
11,264 KB
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
testcase_41 TLE -
testcase_42 AC 69 ms
11,520 KB
testcase_43 AC 67 ms
11,520 KB
testcase_44 AC 68 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/local/bin/pypy
# python 想定解 O(NK)本の辺を張り,下駄をはかせる ライブラリが速い
import heapq

class mcf_graph_int_cost:
    """
    頂点数、及び、costの総和が、4294967295 (== (1 << 32) - 1) を超えない前提下での高速な実装。
    超えると動きません!!!!!
    これらが 4294967295 を超えるときはこっち使ってください。
    https://atcoder.jp/contests/practice2/submissions/18032049
    """
 
    def __init__(self, n):
        self.n = n
        self.pos = []
        self.g = [[] for _ in range(n)]
 
 
    def add_edge(self, from_, to, cap, cost):
        # assert 0 <= from_ < self.n
        # assert 0 <= to < self.n
        m = len(self.pos)
        self.pos.append((from_, len(self.g[from_])))
        self.g[from_].append(self.__class__._edge(to, len(self.g[to]), cap, cost))
        self.g[to].append(self.__class__._edge(from_, len(self.g[from_]) - 1, 0, -cost))
        return m
 
 
    class edge:
        def __init__(self, from_, to, cap, flow, cost):
            self.from_ = from_
            self.to = to
            self.cap = cap
            self.flow = flow
            self.cost = cost
 
 
    def get_edge(self, i):
        _e = self.g[self.pos[i][0]][self.pos[i][1]]
        _re = self.g[_e.to][_e.rev]
        return self.__class__.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap, _e.cost)
 
 
    def edges(self):
        ret = []
        for i in range(len(self.pos)):
            _e = self.g[self.pos[i][0]][self.pos[i][1]]
            _re = self.g[_e.to][_e.rev]
            ret.append(self.__class__.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap, _e.cost))
        return ret
 
 
    def _dual_ref(self, s, t):
        self.dist = [4294967295] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        self.vis = [False] * self.n
 
        que = [s] # s ==  (0 << 32) + s 
        self.dist[s] = 0
        while que:
            v = heapq.heappop(que) & 4294967295
            if self.vis[v]:
                continue
            self.vis[v] = True
            if v == t:
                break
            for i in range(len(self.g[v])):
                e = self.g[v][i]
                if self.vis[e.to] or e.cap == 0:
                    continue
                cost = e.cost - self.dual[e.to] + self.dual[v]
                if self.dist[e.to] > self.dist[v] + cost:
                    self.dist[e.to] = self.dist[v] + cost
                    self.pv[e.to] = v
                    self.pe[e.to] = i
                    heapq.heappush(que, ((self.dist[e.to] << 32) + e.to))
        if not self.vis[t]:
            return False
 
        for v in range(self.n):
            if not self.vis[v]:
                continue
            self.dual[v] -= self.dist[t] - self.dist[v]
        
        return True
 
 
    def slope(self, s, t, flow_limit=4294967295):
        # assert 0 <= s < self.n
        # assert 0 <= t < self.n
        # assert s != t
        
        self.dual = [0] * self.n
        self.dist = [4294967295] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        self.vis = [False] * self.n
        
        flow = 0
        cost = 0
        prev_cost = -1
        result = [(flow, cost)]
        while flow < flow_limit:
            if not self._dual_ref(s, t):
                break
            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.g[self.pv[v]][self.pe[v]].cap)
                v = self.pv[v]
            v = t
            while v != s:
                e = self.g[self.pv[v]][self.pe[v]]
                e.cap -= c
                self.g[v][e.rev].cap += c
                v = self.pv[v]
            d = -self.dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                result.pop()
            result.append((flow, cost))
            prev_cost = cost
        return result
 
 
    def flow(self, s, t, flow_limit=4294967295):
        return self.slope(s, t, flow_limit)[-1]
 
    
    class _edge:
        def __init__(self, to, rev, cap, cost):
            self.to = to
            self.rev = rev
            self.cap = cap
            self.cost = cost

import sys
readline = sys.stdin.readline
write = sys.stdout.write

if __name__ == '__main__':
    BIG = 400
    N, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    P = []
    for i in range(N):
        P.append(list(map(int, readline().split())))

    mcf = mcf_graph_int_cost(2*N+2)
    s = 2*N
    t = s+1

    for i in range(N):
        mcf.add_edge(s, i, A[i], 0)

    S = 0

    for i in range(N):
        for j in range(N):
            S += P[i][j] * P[i][j]
            for x in range(A[i]):
                mcf.add_edge(i, N+j, 1, 2*(x-P[i][j])+1+BIG)

    for i in range(N):
        mcf.add_edge(N+i, t, B[i], 0)

    print(mcf.flow(s, t, K)[1] + S - K*BIG)
0