hdu1011 Starship Troopers
2015-10-22 21:12
267 查看
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]);
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\n");continue; } memset(dp,0,sizeof(dp)); memset(vis,0,sizeof(vis)); dfs(1); printf("%d\n",dp[1][m]); } return 0; }
相关文章推荐
- hadoop伪分布式搭建
- Linux命令行删除文件恢复
- linux交叉编译器的制作
- Spark VS Hadoop
- linux date精简用法
- CentOS6.5升级Python到2.7并安装pip
- 如何写Linux下编译驱动模块的Makefile
- Centos6中安装mysql5.6
- 小白学习shell编程
- Linux用户权限管理
- 一个小白的linux学习历程(1)——第一个脚本
- Linux下修改/设置环境变量JAVA_HOME
- DNS服务部署(三)之高可用主辅架构
- 架构设计:系统间通信(8)——通信管理与RMI 上篇
- tomcat 配置SSL
- 由一个误操作引起的对linux下mv命令的使用总结
- Linux 下的两种分层存储方案
- linux 关于$0$1$n$$$?$#笔记
- Apache Spark 入门简介
- 数据挖掘-Mahout-Canopy聚类实践