結果

問題 No.2604 Initial Motion
ユーザー とりゐとりゐ
提出日時 2024-01-13 14:45:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 5,426 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 82,768 KB
最終ジャッジ日時 2024-09-28 01:30:00
合計ジャッジ時間 16,324 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,632 KB
testcase_01 AC 45 ms
53,248 KB
testcase_02 AC 45 ms
59,392 KB
testcase_03 AC 158 ms
77,444 KB
testcase_04 AC 155 ms
77,564 KB
testcase_05 AC 153 ms
77,772 KB
testcase_06 AC 147 ms
77,500 KB
testcase_07 AC 149 ms
77,600 KB
testcase_08 AC 155 ms
77,632 KB
testcase_09 AC 144 ms
77,432 KB
testcase_10 AC 143 ms
77,316 KB
testcase_11 AC 148 ms
77,440 KB
testcase_12 AC 150 ms
77,352 KB
testcase_13 AC 654 ms
82,612 KB
testcase_14 AC 530 ms
81,684 KB
testcase_15 AC 366 ms
80,012 KB
testcase_16 AC 652 ms
82,060 KB
testcase_17 AC 777 ms
82,720 KB
testcase_18 AC 754 ms
82,768 KB
testcase_19 AC 710 ms
82,580 KB
testcase_20 AC 605 ms
82,292 KB
testcase_21 AC 531 ms
81,448 KB
testcase_22 AC 697 ms
82,436 KB
testcase_23 AC 569 ms
81,944 KB
testcase_24 AC 682 ms
82,352 KB
testcase_25 WA -
testcase_26 AC 605 ms
82,264 KB
testcase_27 AC 515 ms
81,568 KB
testcase_28 AC 630 ms
81,836 KB
testcase_29 AC 648 ms
82,436 KB
testcase_30 AC 526 ms
81,160 KB
testcase_31 AC 596 ms
82,144 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 250 ms
80,900 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 339 ms
80,008 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


class MinCostFlow:
    def __init__(self, n):
        self.n = n
        self._edges = []

    def add_edge(self, from_, to, cap, cost):
        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):
        return self._edges[i]

    def edges(self):
        return self._edges.copy()

    def _dual_ref(self, s, t):
        self.dist = [2147483647] * 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=2147483647):
        assert 0 <= s < self.n
        assert 0 <= t < self.n
        assert s != t

        self._csr()

        self.dual = [0] * self.n
        self.dist = [2147483647] * 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=2147483647):
        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

k,n,m=map(int,input().split())
a=list(map(lambda x:int(x)-1,input().split()))
b=list(map(int,input().split()))

G=MinCostFlow(n+2)
for _ in range(m):
  u,v,d=map(int,input().split())
  u-=1
  v-=1
  G.add_edge(u,v,k,d)
  G.add_edge(v,u,k,d)

S=n
T=n+1
for i in range(k):
  G.add_edge(S,a[i],1,0)
for i in range(n):
  G.add_edge(i,T,b[i],0)

print(G.flow(S,T)[1])
0