結果

問題 No.2604 Initial Motion
ユーザー rlangevinrlangevin
提出日時 2024-01-19 08:57:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,261 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 88,796 KB
最終ジャッジ日時 2024-01-19 08:57:40
合計ジャッジ時間 8,618 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
60,264 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 49 ms
61,724 KB
testcase_03 AC 217 ms
76,024 KB
testcase_04 AC 237 ms
76,032 KB
testcase_05 AC 236 ms
76,164 KB
testcase_06 AC 229 ms
76,032 KB
testcase_07 AC 226 ms
76,032 KB
testcase_08 AC 235 ms
76,028 KB
testcase_09 AC 302 ms
76,152 KB
testcase_10 AC 222 ms
76,028 KB
testcase_11 AC 229 ms
76,024 KB
testcase_12 AC 331 ms
76,176 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class MinCostFlow:
    def __init__(self, N):
        self.N = N
        self.inf = 10 ** 18
        self.G = [[] for i in range(N)]

    def add_edge(self, u, v, cap, cost):
        self.G[u].append((v, cap, cost, len(self.G[v])))
        self.G[v].append((u, 0, -cost, len(self.G[u]) - 1))

    def bellman_ford(self, s):
        dist = [self.inf] * self.N
        dist[s] = 0
        pv = [0] * self.N
        pe = [0] * self.N
        while True:
            update = False
            for v in range(self.N):
                if dist[v] == self.inf:
                    continue
                for i in range(len(self.G[v])):
                    next, cap, cost, _ = self.G[v][i]
                    if cap > 0 and dist[next] > dist[v] + cost:
                        dist[next] = dist[v] + cost
                        update = True
                        pv[next] = v
                        pe[next] = i
            if not update:
                break
        return dist, pv, pe

    def calc_min_cost_flow(self, s, t, f):
        result = 0
        while f > 0:
            dist, pv, pe = self.bellman_ford(s)
            if dist[t] == self.inf:
                return self.inf
            flow = f
            v = t
            while v != s:
                flow = min(flow, self.G[pv[v]][pe[v]][1])
                v = pv[v]
            result += flow * dist[t]
            f -= flow
            v = t
            while v != s:
                d, cap, cost, r = self.G[pv[v]][pe[v]]
                cap -= flow
                self.G[pv[v]][pe[v]] = (d, cap, cost, r)
                rev = self.G[pv[v]][pe[v]][3]
                d, cap, cost, r = self.G[v][rev]
                cap += flow
                self.G[v][rev] = (d, cap, cost, r)
                v = pv[v]
        return result
    
    
K, N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
G = MinCostFlow(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.calc_min_cost_flow(s, g, K))
0