HDU 1011 (tree DP + backpack)

Source: Internet
Author: User

Question Link: Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1011

Theme: Retrieve the vertex from the tree. First retrieve the father and then the son. For each vertex, the weight is W, the cost is cost, and the maximum weight is obtained for the given total m consumption.

Solutions:

Tree Type backpack template question. First, create an undirected graph.

Cost = (Bug [root] + 19)/20 for each point, that is, if the number of bugs is less than 20, you need to send a person.

Use DP [I] [J] to represent the maximum cost of J in the I-root subtree.

Transition equation: DP [I] [J] = max (DP [I] [J], DP [I] [J-K] + dp [T] [k]), t is one of the sons, and K is the cost assigned to the sons.

Use DFS to perform DP for each root.

For each DFS ~ M is initialized. The value is W [root], indicating that only the father is used.

Then perform DFS for each son, each of which is a two for loop.

For (M... j... cost)

For (1... k... j-cost) // The son can allocate up to J-cost

The final result is DP [1] [m], where 1 is the total root point.

Note that m can be equal to 0 in this question. Therefore, After all data is read, 0 is output for the 0 feature; otherwise, DFS is used.

 

#include "cstdio"#include "vector"#include "cstring"using namespace std;#define maxn 500int bug[maxn],w[maxn],head[maxn],n,m,u,v,tol;struct Edge{    int to,next;}e[maxn*2];bool vis[maxn];int dp[maxn][maxn];void addedge(int u,int v){    e[tol].to=v;    e[tol].next=head[u];    head[u]=tol++;}void dfs(int root,int pre){    int i=root,cost=(bug[root]+19)/20;    for(int i=cost;i<=m;i++) dp[root][i]=w[root];    for(int a=head[root];a!=-1;a=e[a].next)    {        int t=e[a].to;        if(t==pre) continue;        dfs(t,root);        for(int j=m;j>=cost;j--)            for(int k=1;k<=j-cost;k++)                dp[i][j]=max(dp[i][j],dp[i][j-k]+dp[t][k]);    }}int main(){    //freopen("in.txt","r",stdin);    while(~scanf("%d%d",&n,&m)&&n>0)    {        memset(head,-1,sizeof(head));        memset(dp,0,sizeof(dp));        tol=0;        for(int i=1;i<=n;i++)            scanf("%d%d",&bug[i],&w[i]);        for(int i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            addedge(u,v);            addedge(v,u);        }        if(m==0) {printf("0\n");continue;}        dfs(1,1);        printf("%d\n",dp[1][m]);    }    return 0;}

 

11488265 12:44:12 Accepted 1011 62 Ms 1280 K 1508 B C ++ Physcal

HDU 1011 (tree DP + backpack)

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.