zoukankan      html  css  js  c++  java
  • hdu1011 Starship Troopers

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


    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
     
    这题可以用树形背包dp做,和poj1155差不多,用dp[i][j]表示节点i及子树用j个troop所得到的最大概率。
    那么转移方程是 dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define inf 99999999
    #define maxn 105
    int first[maxn],vis[maxn],w[maxn],value[maxn];
    int dp[maxn][maxn];
    struct node{
        int to,next;
    }e[2*maxn];
    int n,m;
    
    void dfs(int u)
    {
        int i,j,v,t,k;
        vis[u]=1;
        int flag=0;
        if(w[u]%20==0)t=w[u]/20;
        else t=w[u]/20+1;
        for(i=t;i<=m;i++)dp[u][i]=value[u]; //这里要先初始化为value[u]
    
        for(i=first[u];i!=-1;i=e[i].next){
            v=e[i].to;
            if(vis[v])continue;
            flag=1;
            dfs(v);
            for(j=m;j>=t;j--){
                for(k=j-t;k>0;k--){       //这里k不能等于0,因为如果不派队伍,那么得到的概率一定是0
                    dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
                }
    
            }
    
        }
        for(j=t-1;j>=0;j--){
            dp[u][j]=0;
        }
        if(flag==0){
            if(w[u]%20==0)t=w[u]/20;
            else t=w[u]/20+1;
            for(j=t;j<=m;j++)dp[u][j]=value[u];
        }
    }
    
    
    int main()
    {
        int i,j,c,d,tot;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            if(n==-1 && m==-1)break;
            for(i=1;i<=n;i++){
                scanf("%d%d",&w[i],&value[i]);
            }
            memset(first,-1,sizeof(first));
            tot=0;
            for(i=1;i<=n-1;i++){
                scanf("%d%d",&c,&d);
                tot++;
                e[tot].next=first[c];e[tot].to=d;
                first[c]=tot;
    
                tot++;
                e[tot].next=first[d];e[tot].to=c;
                first[d]=tot;
            }
            if(m==0){
                printf("0
    ");continue;
            }
            memset(dp,0,sizeof(dp));
            memset(vis,0,sizeof(vis));
            dfs(1);
            printf("%d
    ",dp[1][m]);
        }
        return 0;
    }
    


  • 相关阅读:
    asp.net连接mssql server的方式
    mssql如何创建简单的存储过程
    varchar和nvarchar的区别
    C#中关键字partial的作用
    oracle创建用户命令
    在linux上安装音频视频播放器解码器(打造完美播放器)
    oracle创建表空间
    windows console application链接数据库读取数据
    今天学习asp.net mvc的过程中出现了一点问题,是有关浏览器的,一个疑问?
    华为t8100刷机之后出现illegal version 不久开始关机,提示非法版本
  • 原文地址:https://www.cnblogs.com/herumw/p/9464630.html
Copyright © 2011-2022 走看看