1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <bits/stdc++.h> using namespace std; const int N = 3001, M = 3000; int u, x, ww, h[N], w[M], e[M], ne[M], idx; int n, m, v[N], f[N][N]; void add(int u, int v, int ww) { e[++idx] = v, w[idx] = ww, ne[idx] = h[u], h[u] = idx; } int dfs(int x) { if (x > n - m) { f[x][1] = v[x]; return 1; } int sum = 0; for (int i = h[x]; i; i = ne[i]) { int y = e[i]; int s = dfs(y); sum += s; for (int j = sum; j; --j) for (int k = 1; k <= s; ++k) if (j >= k) f[x][j] = max(f[x][j], f[x][j - k] + f[y][k] - w[i]); } return sum; } int main() { scanf("%d%d", &n, &m); memset(f, -0x3f3f3f, sizeof f); register int i = 1; for (; i <= n - m; ++i) { scanf("%d", &x); for (int j = 1; j <= x; ++j) { scanf("%d%d", &u, &ww); add(i, u, ww); } } for (; i <= n; ++i) { scanf("%d", &v[i]); } for (i = 1; i <= n; ++i) f[i][0] = 0; dfs(1); for (i = m; i; --i) if (f[1][i] >= 0) { printf("%d\n", i); break; } return 0; }
|