結果

問題 No.2604 Initial Motion
ユーザー ShirotsumeShirotsume
提出日時 2024-01-12 22:46:49
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,188 bytes
コンパイル時間 298 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 106,356 KB
最終ジャッジ日時 2024-09-27 23:32:26
合計ジャッジ時間 21,830 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
88,964 KB
testcase_01 AC 127 ms
88,832 KB
testcase_02 AC 142 ms
89,320 KB
testcase_03 AC 351 ms
91,896 KB
testcase_04 AC 332 ms
91,260 KB
testcase_05 AC 358 ms
91,136 KB
testcase_06 AC 342 ms
91,264 KB
testcase_07 AC 354 ms
92,032 KB
testcase_08 AC 331 ms
91,520 KB
testcase_09 AC 334 ms
91,136 KB
testcase_10 AC 326 ms
91,136 KB
testcase_11 AC 347 ms
91,288 KB
testcase_12 AC 348 ms
91,648 KB
testcase_13 TLE -
testcase_14 AC 1,880 ms
97,060 KB
testcase_15 AC 1,442 ms
95,744 KB
testcase_16 AC 2,843 ms
101,020 KB
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 AC 2,767 ms
99,884 KB
testcase_21 AC 1,984 ms
98,028 KB
testcase_22 TLE -
testcase_23 AC 2,176 ms
98,552 KB
testcase_24 AC 2,960 ms
101,780 KB
testcase_25 TLE -
testcase_26 AC 2,519 ms
99,664 KB
testcase_27 AC 1,671 ms
96,668 KB
testcase_28 AC 2,374 ms
98,800 KB
testcase_29 TLE -
testcase_30 AC 1,749 ms
96,680 KB
testcase_31 AC 2,424 ms
99,580 KB
testcase_32 AC 2,456 ms
98,552 KB
testcase_33 AC 157 ms
90,024 KB
testcase_34 AC 2,037 ms
100,604 KB
testcase_35 AC 2,098 ms
103,776 KB
testcase_36 AC 2,014 ms
101,784 KB
testcase_37 AC 174 ms
92,212 KB
testcase_38 AC 191 ms
90,772 KB
testcase_39 AC 205 ms
90,756 KB
testcase_40 AC 1,727 ms
104,664 KB
testcase_41 AC 1,614 ms
104,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import fractions
import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 63 - 1
mod = 998244353
from heapq import heappush, heappop
class MinCostFlow:
    INF = 10**18
 
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]
 
    def add_edge(self, fr, to, cap, cost):
        forward = [to, cap, cost, None]
        backward = forward[3] = [fr, 0, -cost, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)
 
    def flow(self, s, t, f):
        N = self.N; G = self.G
        INF = MinCostFlow.INF
 
        res = 0
        H = [0]*N
        prv_v = [0]*N
        prv_e = [None]*N
 
        d0 = [INF]*N
        dist = [INF]*N
 
        while f:
            dist[:] = d0
            dist[s] = 0
            que = [(0, s)]
 
            while que:
                c, v = heappop(que)
                if dist[v] < c:
                    continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w, cap, cost, _ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prv_v[w] = v; prv_e[w] = e
                        heappush(que, (r, w))
            if dist[t] == INF:
                return None
 
            for i in range(N):
                H[i] += dist[i]
 
            d = f; v = t
            while v != s:
                d = min(d, prv_e[v][1])
                v = prv_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prv_e[v]
                e[1] -= d
                e[3][1] += d
                v = prv_v[v]
        return res
k, n, m = mi()

a = li()
C = Counter(a)
b = li()

G = MinCostFlow(n + 2)
S = n
T = n + 1

for i in range(n):
    G.add_edge(S, i, C[i + 1], 0)
    G.add_edge(i, T, b[i], 0)
for _ in range(m):
    u, v, d = mi()
    u -= 1; v -= 1
    G.add_edge(u, v, inf, d)
    G.add_edge(v, u, inf, d)
    
f = G.flow(S, T, k)

print(f)

0