結果

問題 No.2604 Initial Motion
ユーザー rlangevinrlangevin
提出日時 2024-01-19 12:14:02
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,583 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 115,156 KB
最終ジャッジ日時 2024-09-28 03:24:10
合計ジャッジ時間 66,185 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
69,208 KB
testcase_01 AC 66 ms
70,076 KB
testcase_02 AC 70 ms
71,636 KB
testcase_03 AC 256 ms
79,992 KB
testcase_04 AC 277 ms
80,600 KB
testcase_05 AC 249 ms
79,812 KB
testcase_06 AC 253 ms
80,232 KB
testcase_07 AC 229 ms
80,180 KB
testcase_08 AC 279 ms
80,380 KB
testcase_09 AC 247 ms
79,708 KB
testcase_10 AC 253 ms
80,580 KB
testcase_11 AC 288 ms
80,872 KB
testcase_12 AC 277 ms
80,288 KB
testcase_13 AC 2,394 ms
98,184 KB
testcase_14 AC 1,986 ms
91,824 KB
testcase_15 AC 961 ms
86,392 KB
testcase_16 AC 2,538 ms
95,648 KB
testcase_17 AC 2,850 ms
102,912 KB
testcase_18 AC 2,612 ms
103,788 KB
testcase_19 AC 2,507 ms
101,592 KB
testcase_20 AC 2,193 ms
95,812 KB
testcase_21 AC 2,040 ms
92,888 KB
testcase_22 AC 2,514 ms
101,220 KB
testcase_23 AC 2,110 ms
93,616 KB
testcase_24 AC 2,592 ms
96,816 KB
testcase_25 TLE -
testcase_26 AC 2,213 ms
94,576 KB
testcase_27 AC 1,903 ms
90,664 KB
testcase_28 AC 2,166 ms
93,500 KB
testcase_29 AC 2,285 ms
98,096 KB
testcase_30 AC 1,976 ms
91,584 KB
testcase_31 AC 2,194 ms
95,160 KB
testcase_32 AC 2,598 ms
97,360 KB
testcase_33 AC 2,993 ms
93,428 KB
testcase_34 AC 1,402 ms
94,568 KB
testcase_35 AC 2,318 ms
115,156 KB
testcase_36 AC 2,369 ms
111,768 KB
testcase_37 AC 1,059 ms
89,636 KB
testcase_38 AC 132 ms
79,436 KB
testcase_39 AC 146 ms
79,128 KB
testcase_40 AC 2,842 ms
103,040 KB
testcase_41 AC 2,875 ms
103,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
#https://github.com/not522/ac-library-python/blob/master/atcoder/mincostflow.py
from typing import NamedTuple, Optional, List, Tuple, cast
from heapq import heappush, heappop

class MCFGraph:
    class Edge(NamedTuple):
        src: int
        dst: int
        cap: int
        flow: int
        cost: int

    class _Edge:
        def __init__(self, dst: int, cap: int, cost: int) -> None:
            self.dst = dst
            self.cap = cap
            self.cost = cost
            self.rev: Optional[MCFGraph._Edge] = None

    def __init__(self, n: int) -> None:
        self._n = n
        self._g: List[List[MCFGraph._Edge]] = [[] for _ in range(n)]
        self._edges: List[MCFGraph._Edge] = []

    def add_edge(self, src: int, dst: int, cap: int, cost: int) -> int:
        assert 0 <= src < self._n
        assert 0 <= dst < self._n
        assert 0 <= cap
        m = len(self._edges)
        e = MCFGraph._Edge(dst, cap, cost)
        re = MCFGraph._Edge(src, 0, -cost)
        e.rev = re
        re.rev = e
        self._g[src].append(e)
        self._g[dst].append(re)
        self._edges.append(e)
        return m

    def get_edge(self, i: int) -> Edge:
        assert 0 <= i < len(self._edges)
        e = self._edges[i]
        re = cast(MCFGraph._Edge, e.rev)
        return MCFGraph.Edge(
            re.dst,
            e.dst,
            e.cap + re.cap,
            re.cap,
            e.cost
        )

    def edges(self) -> List[Edge]:
        return [self.get_edge(i) for i in range(len(self._edges))]

    def flow(self, s: int, t: int,
             flow_limit: Optional[int] = None) -> Tuple[int, int]:
        return self.slope(s, t, flow_limit)[-1]

    def slope(self, s: int, t: int,
              flow_limit: Optional[int] = None) -> List[Tuple[int, int]]:
        assert 0 <= s < self._n
        assert 0 <= t < self._n
        assert s != t
        if flow_limit is None:
            flow_limit = cast(int, sum(e.cap for e in self._g[s]))

        dual = [0] * self._n
        prev: List[Optional[Tuple[int, MCFGraph._Edge]]] = [None] * self._n

        def refine_dual() -> bool:
            pq = [(0, s)]
            visited = [False] * self._n
            dist: List[Optional[int]] = [None] * self._n
            dist[s] = 0
            while pq:
                dist_v, v = heappop(pq)
                if visited[v]:
                    continue
                visited[v] = True
                if v == t:
                    break
                dual_v = dual[v]
                for e in self._g[v]:
                    w = e.dst
                    if visited[w] or e.cap == 0:
                        continue
                    reduced_cost = e.cost - dual[w] + dual_v
                    new_dist = dist_v + reduced_cost
                    dist_w = dist[w]
                    if dist_w is None or new_dist < dist_w:
                        dist[w] = new_dist
                        prev[w] = v, e
                        heappush(pq, (new_dist, w))
            else:
                return False
            dist_t = dist[t]
            for v in range(self._n):
                if visited[v]:
                    dual[v] -= cast(int, dist_t) - cast(int, dist[v])
            return True

        flow = 0
        cost = 0
        prev_cost_per_flow: Optional[int] = None
        result = [(flow, cost)]
        while flow < flow_limit:
            if not refine_dual():
                break
            f = flow_limit - flow
            v = t
            while prev[v] is not None:
                u, e = cast(Tuple[int, MCFGraph._Edge], prev[v])
                f = min(f, e.cap)
                v = u
            v = t
            while prev[v] is not None:
                u, e = cast(Tuple[int, MCFGraph._Edge], prev[v])
                e.cap -= f
                assert e.rev is not None
                e.rev.cap += f
                v = u
            c = -dual[s]
            flow += f
            cost += f * c
            if c == prev_cost_per_flow:
                result.pop()
            result.append((flow, cost))
            prev_cost_per_flow = c
        return result
    
    
K, N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
G = MCFGraph(N + 2)
s, g = 0, N + 1
for i in range(M):
    u, v, d = map(int, input().split())
    G.add_edge(u, v, K, d)
    G.add_edge(v, u, K, d)
    
for i in range(K):
    G.add_edge(s, A[i], 1, 0)
for i in range(N):
    G.add_edge(i + 1, g, B[i], 0)
    
print(G.flow(s, g, K)[1])
0