救命 PY3树形DP怎么只过了一个点

P1064 [NOIP2006 提高组] 金明的预算方案

izudia @ 2023-04-07 19:12:35

from collections import defaultdict

def dfs(u):
    for i in range(v[u], n + 1):
        f[u][i] = w[u]
    for s in e[u]:
        dfs(s)
        for j in range(n, v[u] - 1, -1):
            for k in range(j - v[u] + 1):
                f[u][j] = max(f[u][j], f[u][j - k] + f[s][k])
    return

n, m = map(int, input().split())
v, p, q, w = [0] * (m + 1), [0] * (m + 1), [0] * (m + 1), [0] * (m + 1)
e = defaultdict(list)

for i in range(1, m + 1):
    v[i], p[i], q[i] = map(int, input().split())
    w[i] = v[i] * p[i]
    e[q[i]].append(i)

f = [[0] * (n + 1) for _ in range(m + 1)]
dfs(0)
print(max(f[0]))

是DFS没经过剪枝的缘故嘛,还是单纯PY3太慢了,好多题PY3都过不100pts


|