結果

問題 No.2604 Initial Motion
ユーザー detteiuudetteiuu
提出日時 2024-12-04 00:13:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 977 ms / 3,000 ms
コード長 8,343 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 82,624 KB
実行使用メモリ 82,528 KB
最終ジャッジ日時 2024-12-04 00:17:19
合計ジャッジ時間 19,343 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,452 KB
testcase_01 AC 38 ms
55,292 KB
testcase_02 AC 40 ms
55,000 KB
testcase_03 AC 145 ms
77,956 KB
testcase_04 AC 145 ms
78,348 KB
testcase_05 AC 144 ms
77,604 KB
testcase_06 AC 140 ms
77,496 KB
testcase_07 AC 139 ms
77,824 KB
testcase_08 AC 147 ms
78,204 KB
testcase_09 AC 136 ms
77,772 KB
testcase_10 AC 137 ms
77,820 KB
testcase_11 AC 141 ms
78,244 KB
testcase_12 AC 139 ms
77,632 KB
testcase_13 AC 639 ms
81,924 KB
testcase_14 AC 526 ms
81,000 KB
testcase_15 AC 364 ms
80,684 KB
testcase_16 AC 657 ms
82,272 KB
testcase_17 AC 790 ms
82,140 KB
testcase_18 AC 748 ms
82,140 KB
testcase_19 AC 686 ms
82,248 KB
testcase_20 AC 591 ms
81,700 KB
testcase_21 AC 518 ms
81,060 KB
testcase_22 AC 690 ms
82,452 KB
testcase_23 AC 570 ms
81,588 KB
testcase_24 AC 690 ms
81,800 KB
testcase_25 AC 536 ms
82,116 KB
testcase_26 AC 574 ms
81,416 KB
testcase_27 AC 513 ms
81,232 KB
testcase_28 AC 616 ms
81,500 KB
testcase_29 AC 628 ms
81,664 KB
testcase_30 AC 538 ms
80,952 KB
testcase_31 AC 572 ms
80,908 KB
testcase_32 AC 471 ms
81,032 KB
testcase_33 AC 827 ms
81,416 KB
testcase_34 AC 240 ms
80,924 KB
testcase_35 AC 645 ms
82,344 KB
testcase_36 AC 416 ms
81,648 KB
testcase_37 AC 293 ms
80,052 KB
testcase_38 AC 89 ms
76,660 KB
testcase_39 AC 85 ms
76,176 KB
testcase_40 AC 977 ms
82,528 KB
testcase_41 AC 907 ms
82,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://atcoder.jp/contests/practice2/submissions/20654283
import heapq

class mcf_graph_int_cost:
    """It solves Minimum-cost flow problem.
    
    """

    def __init__(self, n):
        """It creates a directed graph with n vertices and 0 edges.
        Cap and Cost are the type of the capacity and the cost, respectively.

        Constraints
        -----------

        >   0 <= n <= 10 ** 8

        >   Cap and Cost are int.

        Complexity
        ----------

        >   O(n)
        """
        self.n = n
        self._edges = []

    def add_edge(self, from_, to, cap, cost):
        """It adds an edge oriented from `from_` to `to` with capacity `cap` and cost `cost`. 
        It returns an integer k such that this is the k-th edge that is added.

        Constraints
        -----------

        >   0 <= from_, to < n

        >   0 <= cap, cost

        Complexity
        ----------

        >   O(1) amortized
        """
        # assert 0 <= from_ < self.n
        # assert 0 <= to < self.n
        # assert 0 <= cap
        # assert 0 <= cost
        m = len(self._edges)
        self._edges.append(self.__class__.edge(from_, to, cap, 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):
        """It returns the current internal state of the edges.
        The edges are ordered in the same order as added by add_edge.

        Constraints
        -----------

        >   0 <= i < m

        Complexity
        ----------

        >   O(1)
        """
        # assert 0 <= i < len(self._edges)
        return self._edges[i]

    def edges(self):
        """It returns the current internal state of the edges.
        The edges are ordered in the same order as added by add_edge.

        Complexity
        ----------

        >   O(m), where m is the number of added edges.
        """
        return self._edges.copy()

    def _dual_ref(self, s, t):
        self.dist = [10**18] * self.n
        self.vis = [False] * self.n
        self.que_min.clear()
        self.que.clear()
        que_push_que = []
        self.dist[s] = 0
        self.que_min.append(s)
        while self.que_min or self.que or que_push_que:
            if self.que_min:
                v = self.que_min.pop()
            else:
                while que_push_que:
                    heapq.heappush(self.que, que_push_que.pop())
                v = heapq.heappop(self.que) & 4294967295
            if self.vis[v]:
                continue
            self.vis[v] = True
            if v == t:
                break
            dual_v = self.dual[v]
            dist_v = self.dist[v]
            for i in range(self.start[v], self.start[v + 1]):
                e = self.elist[i]
                if not e.cap:
                    continue
                cost = e.cost - self.dual[e.to] + dual_v
                if self.dist[e.to] - dist_v > cost:
                    dist_to = dist_v + cost
                    self.dist[e.to] = dist_to
                    self.prev_e[e.to] = e.rev
                    if dist_to == dist_v:
                        self.que_min.append(e.to)
                    else:
                        que_push_que.append((dist_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 _csr(self):
        m = len(self._edges)
        self.edge_idx = [0] * m
        redge_idx = [0] * m
        degree = [0] * self.n
        edges = []
        for i, e in enumerate(self._edges):
            self.edge_idx[i] = degree[e.from_]
            degree[e.from_] += 1
            redge_idx[i] = degree[e.to]
            degree[e.to] += 1
            edges.append((e.from_, self.__class__._edge(
                e.to, -1, e.cap - e.flow, e.cost)))
            edges.append((e.to, self.__class__._edge(
                e.from_, -1, e.flow, -e.cost)))
        self.start = [0] * (self.n + 1)
        self.elist = [0] * len(edges)
        for v, w in edges:
            self.start[v + 1] += 1
        for i in range(1, self.n + 1):
            self.start[i] += self.start[i-1]
        counter = self.start.copy()
        for v, w in edges:
            self.elist[counter[v]] = w
            counter[v] += 1
        for i, e in enumerate(self._edges):
            self.edge_idx[i] += self.start[e.from_]
            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=10**18):
        """Let g be a function such that g(x) is the cost of the minimum cost s-t flow 
        when the amount of the flow is exactly x. 
        g is known to be piecewise linear.
        It returns g as the list of the changepoints, that satisfies the followings.

        >   The first element of the list is (0, 0).

        >   Both of element[0] and element[1] are strictly increasing.

        >   No three changepoints are on the same line.

        >   (1) The last element of the list is (x, g(x)), where x is the maximum amount of the s-t flow.

        >   (2) The last element of the list is (y, g(y)), where y = min(x, flow_limit).

        Constraints
        -----------

        Let x be the maximum cost among all edges.

        >   s != t

        >   You can't call min_cost_slope or min_cost_max_flow multiple times.

        >   0 <= nx <= 2 * 10 ** 9 + 1000

        Complexity
        ----------

        >   O(F (n + m) log (n + m)), where F is the amount of the flow and m is the number of added edges.
        """
        # assert 0 <= s < self.n
        # assert 0 <= t < self.n
        # assert s != t

        self._csr()

        self.dual = [0] * self.n
        self.dist = [10**18] * self.n
        self.prev_e = [0] * self.n
        self.vis = [False] * self.n

        flow = 0
        cost = 0
        prev_cost_per_flow = -1
        result = [(0, 0)]
        self.que = []
        self.que_min = []
        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

        for i in range(len(self._edges)):
            e = self.elist[self.edge_idx[i]]
            self._edges[i].flow = self._edges[i].cap - e.cap

        return result

    def flow(self, s, t, flow_limit=10**18):
        """It augments the flow from s to t as much as possible. 
        It returns the amount of the flow and the cost.

        (1) It augments the s-t flow as much as possible.

        (2) It augments the s-t flow as much as possible, until reaching the amount of flow_limit. 

        Constraints
        -----------

        >   same as mcf_graph.slope

        Complexity
        ----------

        >   same as mcf_graph.slope
        """
        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

INF = 10**9

K, N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
edge = [list(map(int, input().split())) for _ in range(M)]

MCF = mcf_graph_int_cost(N+2)
for i in range(K):
    MCF.add_edge(N, A[i]-1, 1, 0)
for u, v, d in edge:
    MCF.add_edge(u-1, v-1, INF, d)
    MCF.add_edge(v-1, u-1, INF, d)
for i in range(N):
    MCF.add_edge(i, N+1, B[i], 0)

print(MCF.flow(N, N+1, K)[1])
0