結果

問題 No.2604 Initial Motion
ユーザー ニックネームニックネーム
提出日時 2024-01-12 22:30:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 851 ms / 3,000 ms
コード長 6,488 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 82,540 KB
実行使用メモリ 82,980 KB
最終ジャッジ日時 2024-09-27 23:05:23
合計ジャッジ時間 15,876 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,588 KB
testcase_01 AC 39 ms
55,800 KB
testcase_02 AC 41 ms
57,104 KB
testcase_03 AC 124 ms
77,540 KB
testcase_04 AC 128 ms
77,772 KB
testcase_05 AC 129 ms
77,304 KB
testcase_06 AC 127 ms
77,516 KB
testcase_07 AC 136 ms
78,508 KB
testcase_08 AC 128 ms
77,896 KB
testcase_09 AC 125 ms
77,640 KB
testcase_10 AC 126 ms
77,536 KB
testcase_11 AC 130 ms
77,660 KB
testcase_12 AC 129 ms
77,640 KB
testcase_13 AC 548 ms
81,712 KB
testcase_14 AC 413 ms
80,860 KB
testcase_15 AC 328 ms
80,420 KB
testcase_16 AC 485 ms
81,648 KB
testcase_17 AC 661 ms
82,436 KB
testcase_18 AC 600 ms
81,956 KB
testcase_19 AC 568 ms
82,156 KB
testcase_20 AC 469 ms
81,620 KB
testcase_21 AC 408 ms
80,956 KB
testcase_22 AC 554 ms
81,888 KB
testcase_23 AC 438 ms
80,756 KB
testcase_24 AC 581 ms
81,536 KB
testcase_25 AC 431 ms
81,416 KB
testcase_26 AC 469 ms
81,424 KB
testcase_27 AC 370 ms
80,672 KB
testcase_28 AC 447 ms
81,160 KB
testcase_29 AC 519 ms
82,004 KB
testcase_30 AC 373 ms
80,700 KB
testcase_31 AC 480 ms
81,332 KB
testcase_32 AC 337 ms
80,644 KB
testcase_33 AC 111 ms
78,620 KB
testcase_34 AC 225 ms
81,040 KB
testcase_35 AC 657 ms
81,960 KB
testcase_36 AC 660 ms
82,704 KB
testcase_37 AC 132 ms
79,936 KB
testcase_38 AC 88 ms
76,352 KB
testcase_39 AC 89 ms
76,600 KB
testcase_40 AC 851 ms
82,980 KB
testcase_41 AC 807 ms
82,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://atcoder.jp/contests/practice2/submissions/33588016
import heapq
from heapq import heappush, heappop

class MinCostFlow:
    """
    https://github.com/atcoder/ac-library/blob/master/atcoder/internal_csr.hpp
    https://github.com/atcoder/ac-library/blob/master/atcoder/mincostflow.hpp
    https://github.com/atcoder/ac-library/blob/master/document_en/mincostflow.md
    https://github.com/atcoder/ac-library/blob/master/document_ja/mincostflow.md
    """
    def __init__(self, n):
        self.n = n
        self._edges = []

    def add_edge(self, fr, to, cap, cost):
        assert 0 <= fr < self.n
        assert 0 <= to < self.n
        assert 0 <= cap
        assert 0 <= cost
        self._edges.append(self.edge(fr, to, cap, cost))
        return len(self._edges) - 1

    class edge:
        def __init__(self, fr, to, cap, cost):
            self.fr = fr
            self.to = to
            self.cap = cap
            self.flow = 0
            self.cost = cost

    def get_edge(self, i):
        assert 0 <= i < len(self._edges)
        return self._edges[i]

    def edges(self):
        return self._edges

    def flow(self, s, t, flow_limit=1<<60):
        return self.slope(s, t, flow_limit)[-1]

    def __csr(self, edges):
        # Compressed Sparse Row
        self.start = [0] * (self.n + 1)
        for fr, _ in edges:
            self.start[fr + 1] += 1
        for i in range(self.n):
            self.start[i + 1] += self.start[i]
        counter = self.start.copy()
        self.elist = [0] * len(edges)
        for fr, e in edges:
            self.elist[counter[fr]] = e
            counter[fr] += 1

    class _edge:
        def __init__(self, to, rev, cap, cost):
            self.to = to
            self.rev = rev
            self.cap = cap
            self.cost = cost

    def __g(self):
        degree = [0] * self.n
        redge_idx = [0] * self.m
        elist = [(0, None)] * (2 * self.m)
        now_elist = 0
        for i in range(self.m):
            e = self._edges[i]
            self.edge_idx[i] = degree[e.fr]
            degree[e.fr] += 1
            redge_idx[i] = degree[e.to]
            degree[e.to] += 1
            elist[now_elist] = (e.fr, self._edge(e.to, -1, e.cap - e.flow, e.cost))
            now_elist += 1
            elist[now_elist] = (e.to, self._edge(e.fr, -1, e.flow, -e.cost))
            now_elist += 1
        self.__csr(elist)
        for i in range(self.m):
            e = self._edges[i]
            self.edge_idx[i] += self.start[e.fr]
            redge_idx[i] += self.start[e.to]
            self.elist[self.edge_idx[i]].rev = redge_idx[i]
            self.elist[redge_idx[i]].rev = self.edge_idx[i]

    def slope(self, s, t, flow_limit=1<<60):
        assert 0 <= s < self.n
        assert 0 <= t < self.n
        assert s != t
        self.m = len(self._edges)
        self.edge_idx = [0] * self.m
        self.__g()
        result = self.__slope(s, t, flow_limit)
        for i in range(self.m):
            e = self.elist[self.edge_idx[i]]
            self._edges[i].flow = self._edges[i].cap - e.cap
        return result

    def __dual_ref(self, s, t):
        log = self.n.bit_length()
        mask = (1<<log) - 1
        dist = [1<<60] * self.n
        vis = [0] * self.n
        que_min = []
        que = []
        dist[s] = 0
        que_min.append(s)
        while que_min or que:
            if que_min:
                v = que_min.pop()
            else:
                v = heappop(que) & mask
            if vis[v]:
                continue
            vis[v] = 1
            if v == t:
                break
            # dist[v] = shortest(s, v) + dual[s] - dual[v]
            # dist[v] >= 0 (all reduced cost are positive)
            # dist[v] <= (n-1)C
            dual_v = self.dual[v]
            dist_v = dist[v]
            for i in range(self.start[v], self.start[v+1]):
                e = self.elist[i]
                if not e.cap:
                    continue
                # |-dual[e.to] + dual[v]| <= (n-1)C
                # cost <= C - -(n-1)C + 0 = nC
                cost = e.cost - self.dual[e.to] + dual_v
                if dist[e.to] - dist_v > cost:
                    dist_to = dist_v + cost
                    dist[e.to] = dist_to
                    self.prev_e[e.to] = e.rev
                    if dist_to == dist_v:
                        que_min.append(e.to)
                    else:
                        heappush(que, dist_to<<log | e.to)
        if not vis[t]:
            return False
        for v in range(self.n):
            if not vis[v]:
                continue
            # dual[v]
            # = dual[v] - dist[t] + dist[v]
            # = dual[v] - (shortest(s, t) + dual[s] - dual[t]) + (shortest(s, v) + dual[s] - dual[v])
            # = - shortest(s, t) + dual[t] + shortest(s, v)
            # = shortest(s, v) - shortest(s, t)
            # >= 0 - (n-1)C
            self.dual[v] -= dist[t] - dist[v]
        return True

    def __slope(self, s, t, flow_limit):
        # variants (C = maxcost):
        # -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
        # reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge
        self.dual = [0] * self.n
        self.prev_e = [0] * self.n
        flow = 0
        cost = 0
        prev_cost_per_flow = -1
        result = [(0, 0)]
        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.elist[self.elist[self.prev_e[v]].rev].cap)
                v = self.elist[self.prev_e[v]].to
            v = t
            while v != s:
                e = self.elist[self.prev_e[v]]
                e.cap += c
                self.elist[e.rev].cap -= c
                v = self.elist[self.prev_e[v]].to
            d = -self.dual[s]
            flow += c
            cost += c * d
            if prev_cost_per_flow == d:
                result.pop()
            result.append((flow, cost))
            prev_cost_per_flow = d
        return result

from collections import Counter
k,n,m = map(int,input().split())
a = Counter(map(int,input().split()))
b = list(map(int,input().split()))
g = MinCostFlow(n+2)
for i,v in a.items(): g.add_edge(0,i,v,0)
for i,v in enumerate(b,1): g.add_edge(i,n+1,v,0)
for _ in range(m):
    u,v,d = map(int,input().split())
    g.add_edge(u,v,k,d); g.add_edge(v,u,k,d)
print(g.flow(0,n+1)[1])
0