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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <cstdio> #include <algorithm> using namespace std; const int N = 3e3 + 10, M = 3e3 + 10, inf = 3e3; int n, m, g[N], f[N][N]; int idx, hd[N], nxt[M], edg[M], wt[M]; int dfs(int x) { if (x > n - m) return 1; int s = 0; for (int i = hd[x]; ~i; i = nxt[i]) { int mx = dfs(edg[i]); for (int j = 0; j <= s; j++) g[j] = f[x][j]; for (int j = 0; j <= s + mx; j++) for (int k = 0; k <= s; k++) f[x][j] = max(f[x][j], g[k] + f[edg[i]][j - k] - wt[i]); s += mx; } return s; } void add(int a, int b, int c) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; wt[idx] = c; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) hd[i] = -1; for (int i = 1, k; i <= n - m; i++) { scanf("%d", &k); for (int a, c; k; k--) { scanf("%d%d", &a, &c); add(i, a, c); } } for (int i = 1; i <= n; i++) { f[i][0] = 0; for (int j = 1; j <= m; j++) f[i][j] = -inf; } for (int i = n - m + 1, s; i <= n; i++) { scanf("%d", &s); f[i][1] = s; } dfs(1); for (int i = m; i >= 0; i--) if (f[1][i] >= 0) { printf("%d", i); break; } return 0; }
|