Poj 1155 tele (tree DP)

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.