complete 1005.py and 2294.py

This commit is contained in:
2024-05-20 19:27:01 +09:00
parent 69073a4b7e
commit 3ddc5f605f
2 changed files with 75 additions and 0 deletions

38
zeta_python/2294.py Normal file
View File

@@ -0,0 +1,38 @@
import sys
import os
import io
sys.setrecursionlimit(10001)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
P: list
N: int
K: int
Mem: list
def get_possible(C):
if Mem[C] != -1:
return Mem[C]
m = float("inf")
for p in P:
if C - p > 0:
m = min([get_possible(C - p) + 1, m])
elif C == p:
m = 1
elif C - p < 0:
continue
Mem[C] = m
return m
if __name__ == "__main__":
N, K = map(int, input().split())
P = [int(input()) for _ in range(N)]
Mem = [-1] * (K + 1)
ret = get_possible(K)
if ret is float("inf"):
print(-1)
else:
print(ret)

View File

@@ -0,0 +1,37 @@
import sys
import os
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
sys.setrecursionlimit(1000000)
Mem: list
D: list
E: list
def get_time(now):
if Mem[now] != -1:
return Mem[now]
elif not E[now]:
Mem[now] = D[now]
return D[now]
else:
s = max([get_time(target) for target in E[now]]) + D[now]
Mem[now] = s
return s
if __name__ == "__main__":
T = int(input())
for _ in range(T):
N, K = map(int, input().split())
D = [0] + list(map(int, input().split()))
E = [[] for _ in range(N + 1)]
for _ in range(K):
u, v = map(int, input().split())
E[v].append(u)
W = int(input())
Mem = [-1] * (N + 1)
print(get_time(W))