A TV station broadcast program. The Broadcast Network is represented by a tree. Node 1 indicates a station, and leaf node indicates a user. The user is willing to pay for the program, from non-leaf nodes to other nodes requires a certain amount of money (that is, some money is required from the relay point to another relay point). In the end, if you do not lose money, the maximum number of people who can watch the program.
Define DP [I] [J] to indicate that under the subtree where node I is the root node, the maximum profit of the personal watching node of J is exceeded.
The state transition equation is DP [I] [J] = max (DP [I] [J], DP [I] [J-K] + dp [son of I] [k])
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; # define Inf (1 <30) const int n = 3005; struct edge {int V, w; edge * NXT;} memo [N * n], * cur, * adj [N]; int DP [N] [N], n, m; int num [N]; void addedge (int u, int V, int W) {cur-> V = V; cur-> W = W; cur-> NXT = adj [u]; adj [u] = cur ++;} void DFS (int u, int FA) {for (edge * It = adj [u]; it = it-> NXT) {int v = it-> V, W = it-> W; if (V = FA) continue; DFS (v, U); num [u] + = num [v]; for (int I = num [u]; I> = 0; I --) for (Int J = 0; j <= I; j ++) if (DP [u] [I-j]! =-INF & DP [v] [J]! =-INF) DP [u] [I] = max (DP [u] [I], DP [u] [I-j] + dp [v] [J]-W) ;}} void Init () {cur = memo; memset (adj, 0, sizeof (adj); memset (Num, 0, sizeof (Num); For (INT I = 0; I <= N; I ++) {DP [I] [0] = 0; For (Int J = 1; j <= m; j ++) DP [I] [J] =-INF ;}} int main () {While (scanf ("% d", & N, & M )! = EOF) {Init (); int V, W, K; For (INT I = 1; I <= N-m; I ++) {scanf ("% d", & K); For (Int J = 0; j <K; j ++) {scanf ("% d", & V, & W); addedge (I, V, W); addedge (V, I, W) ;}} for (INT I = N-m + 1; I <= N; I ++) scanf ("% d", & DP [I] [1]), num [I] = 1; DFS (1,-1 ); for (INT I = m; I> = 0; I --) {If (DP [1] [I]> = 0) {printf ("% d \ n ", i); break ;}}return 0 ;}