• HDU Starship Troopers (树形DP)


    Starship Troopers

    Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 1   Accepted Submission(s) : 1

    Font: Times New Roman | Verdana | Georgia

    Font Size:  

    Problem Description

    You, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupied by some bugs, and their brains hide in some of the rooms. Scientists have just developed a new weapon and want to experiment it on some brains. Your task is to destroy the whole base, and capture as many brains as possible.

    To kill all the bugs is always easier than to capture their brains. A map is drawn for you, with all the rooms marked by the amount of bugs inside, and the possibility of containing a brain. The cavern's structure is like a tree in such a way that there is one unique path leading to each room from the entrance. To finish the battle as soon as possible, you do not want to wait for the troopers to clear a room before advancing to the next one, instead you have to leave some troopers at each room passed to fight all the bugs inside. The troopers never re-enter a room where they have visited before.

    A starship trooper can fight against 20 bugs. Since you do not have enough troopers, you can only take some of the rooms and let the nerve gas do the rest of the job. At the mean time, you should maximize the possibility of capturing a brain. To simplify the problem, just maximize the sum of all the possibilities of containing brains for the taken rooms. Making such a plan is a difficult job. You need the help of a computer.

    Input

    The input contains several test cases. The first line of each test case contains two integers N (0 < N <= 100) and M (0 <= M <= 100), which are the number of rooms in the cavern and the number of starship troopers you have, respectively. The following N lines give the description of the rooms. Each line contains two non-negative integers -- the amount of bugs inside and the possibility of containing a brain, respectively. The next N - 1 lines give the description of tunnels. Each tunnel is described by two integers, which are the indices of the two rooms it connects. Rooms are numbered from 1 and room 1 is the entrance to the cavern.

    The last test case is followed by two -1's.

    Output

    For each test case, print on a single line the maximum sum of all the possibilities of containing brains for the taken rooms.

    Sample Input

    5 10
    50 10
    40 10
    40 20
    65 30
    70 30
    1 2
    1 3
    2 4
    2 5
    1 1
    20 7
    -1 -1
    

    Sample Output

    50
    7
    

    Author

    XU, Chuan

    Source

    ZJCPC2004
     
     

    题目大意:给出一张树形的地图,然后每个节点就是一个洞,每个洞里面有bugs和brain,现在给出我方有m个骑兵,每个骑兵可以消灭20个bugs,经过的洞穴不能再回头,要收服每个洞穴里面的brain就必须消灭每个洞穴里面的全部bugs,求最大的brain

    思路:树形DP.

    对于x个节点来说.dp[x][i]表示在x洞穴用i个骑兵可以收服的brain的数目.其实就是01背包的稍加变形.

    for(i=1;i<=n;i++)

      for(int j=m;j>=r;j--)

          for(int k=1;k<=j-r;j++)                                    //k<=j-r 即是:k+r<=j,即等会用于:对于当前节点使用剩下的骑兵k在子节点能够收服的brain值.

                  dp[x][j]=max(dp[x][j],dp[x][j-k]+dp[i][k]);// i是x的子节点.这就是说:x节点收服brain的最大值等于:  max(本身初始化的值 ,本身+剩下骑兵的情况下子节点收服的brain值)

    整个过程DFS下去再回朔,不用标记,因为这是树,只需要标记下前缀就可以了.因为数据有可能是:反树的方向给出,

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int VM=110;
    
    struct Edge{
        int to,nxt;
    }edge[VM<<1];
    
    int n,m,cnt,head[VM];
    int vis[VM],bug[VM],brain[VM],dp[VM][VM];   //dp[x][j]表示在i洞派出j个骑兵获取的brain值 
    
    void addedge(int cu,int cv){    //树建立双向边,是为了保证两点的连通
        edge[cnt].to=cv;    edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
        edge[cnt].to=cu;    edge[cnt].nxt=head[cv];
        head[cv]=cnt++;
    }
    
    void DFS(int u){
        if(vis[u])
            return ;
        vis[u]=1;
        int num=(bug[u]+19)/20;
        for(int j=m;j>=num;j--)  //初始化很重要,因为是边界条件
            dp[u][j]=brain[u];      //进入这个洞,只要派出的人数不小于这个洞的bug的 ,都可以至少获得这个洞brain值这么大的brain
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(!vis[v]){
                DFS(v);
                for(int j=m;j>=num;j--)
                    for(int k=1;k<=j-num;k++)
                        dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
            }
        }
    }
    
    int main(){
    
        freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            if(n==-1 && m==-1)
                break;
            cnt=0;
            memset(head,-1,sizeof(head));
            for(int i=1;i<=n;i++)
                scanf("%d%d",&bug[i],&brain[i]);
            int u,v;
            for(int i=1;i<n;i++){
                scanf("%d%d",&u,&v);
                addedge(u,v);
            }
            if(m==0){
                printf("0\n");
                continue;
            }
            memset(vis,0,sizeof(vis));
            memset(dp,0,sizeof(dp));
            DFS(1);
            printf("%d\n",dp[1][m]);
        }
        return 0;
    }

  • 相关阅读:
    推荐体系算法总结
    Springboot 多模块调用,找不到注入的类
    LRU算法
    在SQLServer中连接另一个SQLServer库数据,在Oracle中连接另一个Oracle库数据,在SQL Server中连接Oracle数据,在Oracle中连接SQL Server数据
    用C#实现木马程序
    CSS filter 滤镜可视化配置
    微信小程序农历日期选择器 lunarpicker
    ms Sql 数据库出现 “提供的统计信息流已损坏”的解决办法。
    自制《要塞:十字军东征》无限金钱修改器
    c#+Winform实现自定义的“复制、粘贴”右键快捷菜单,多个控件共享使用一个右键菜单。
  • 原文地址:https://www.cnblogs.com/jackge/p/3051552.html
Copyright © 2020-2023  润新知