zoukankan      html  css  js  c++  java
  • hdu 1011 Starship Troopers 经典的树形DP ****

    Starship Troopers

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8540    Accepted Submission(s): 2379

    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
     
    题意:消灭怪物,获取大脑。==》 花费wi ,得到价值val,是一颗树,入口是1.
         没有告诉你谁是根节点,输入顺序也没有说,谁是谁的父亲节点。
     
    思路:构建双向图,然后在搜索的时候用一个use[]标记是否该节点出现过,来避免死循环的出现。
             这样图就能建立起来了。
             谁是根节点呢,其实,在一颗树,你以任一点为一棵树,都不会破坏,边的关系。
             所以直接从入口出发。1.
           
             状态转移方程:dp[ k ] [ j ] 代表着以 k 为根节点,用了 k 个士兵 得到的最大价值。
             题意已经告诉我们,j的最大值是M 。
             如何初始化? 这个很关键,而且这道题,存在依赖关系。
             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.
             初始化: int num=(w[k]+19)/20;      for(i=num;i<=m;i++) dp[ k ] [ i ]=val[ k ];
             通式:     i f (  dp[ k ] [ j-s ] !=-1  ) //必须要满足父亲节点里的  怪物  被消灭呀。
                      dp[ k ] [ j ]= max(   dp[ k ] [ j ], dp[ k ] [ j-s ]+ dp [ t ] [ s ]  )      
       
     
             数据会出现几个特例,
             wi vi = 0  0  这使得我们要用初始化 memset(dp,-1,sizeof(dp));
             if( M==0 )  printf("0 ");
             ...
            
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6 int n,m;
     7 int w[102],val[102];
     8 struct node
     9 {
    10     int next[102];
    11     int num;
    12 }f[102];
    13 int dp[102][102];
    14 bool use[102];
    15 
    16 int Max(int x,int y)
    17 {
    18     return x>y? x:y;
    19 }
    20 
    21 void dfs(int k)
    22 {
    23     int i,j,t,s;
    24     use[k]=true;
    25     int num=(w[k]+19)/20;
    26     for(i=num;i<=m;i++) dp[k][i]=val[k];
    27 
    28     for(i=1;i<=f[k].num;i++)
    29     {
    30         t=f[k].next[i];
    31         if(use[t]==true) continue;
    32         dfs(t);
    33         for(j=m;j>=num;j--)
    34         {
    35             for(s=1;s<=j;s++)
    36             {
    37                 if(dp[k][j-s]!=-1)
    38                 dp[k][j]=Max(dp[k][j],dp[k][j-s]+dp[t][s]);
    39             }
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     int i,x,y;
    47     while(scanf("%d%d",&n,&m)>0)
    48     {
    49         if(n==-1&&m==-1)break;
    50         memset(dp,-1,sizeof(dp));
    51         memset(use,false,sizeof(use));
    52 
    53         for(i=0;i<=100;i++)
    54         f[i].num=0;
    55 
    56         for(i=1;i<=n;i++)
    57         scanf("%d%d",&w[i],&val[i]);
    58         for(i=1;i<n;i++)
    59         {
    60             scanf("%d%d",&x,&y);
    61             f[x].num++;
    62             f[x].next[f[x].num]=y;
    63 
    64             f[y].num++;
    65             f[y].next[f[y].num]=x;
    66         }
    67         if(m==0)
    68         {
    69             printf("0
    ");
    70             continue;
    71         }
    72         dp[1][m]=0;
    73         /*
    74             1 1
    75             21 1    --> 0
    76         */
    77         dfs(1);
    78         printf("%d
    ",dp[1][m]);
    79 
    80     }
    81     return 0;
    82 }
     
     
     
     
     
  • 相关阅读:
    第二十节:Scrapy爬虫框架之使用Pipeline存储
    第十九节:Scrapy爬虫框架之Middleware文件详解
    第十八节:Scrapy爬虫框架之settings文件详解
    第十七节:Scrapy爬虫框架之item.py文件以及spider中使用item
    第十六节:Scrapy爬虫框架之项目创建spider文件数据爬取
    第一节:python提取PDF文档中的图片
    第十五节:Web爬虫之selenium动态渲染爬取
    H5移动开发底部导航-博客园老牛大讲堂
    ajax跨域通信-博客园老牛大讲堂
    博客园模板2--博客园老牛大讲堂
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3396083.html
Copyright © 2011-2022 走看看