HDOJ--1011
来源:互联网 发布:解方程组软件 编辑:程序博客网 时间:2024/05/22 00:43
Starship Troopers
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15015 Accepted Submission(s): 4039
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.
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.
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 1050 1040 1040 2065 3070 301 21 32 42 51 120 7-1 -1
Sample Output
507
当时看到这题并不觉得简单,很明显的树形DP,花了一个晚自习终于AC了。这样的题只有做多了才会有感觉,很麻烦的一道题。
以下是我的AC代码:
//树形DP--Starship Troopers#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<vector> using namespace std;struct node{ int x,y;}a[110];vector<int> b[110];int NUM_ROOM,NUM_SHIP,MAX_POS;int START_ROOM,GET_ROOM;int COST_SHIP[110];int dp[110][110];int POS_brain[110];void dfs(int START_ROOM,int pre){ for(int i=COST_SHIP[START_ROOM];i<=NUM_SHIP;++i) { dp[START_ROOM][i]=POS_brain[START_ROOM]; } int num=b[START_ROOM].size(); for(int i=0;i<num;++i) { int v=b[START_ROOM][i]; if(v==pre) continue; dfs(v,START_ROOM); for(int j=NUM_SHIP;j>=COST_SHIP[START_ROOM];--j) { for(int k=1;k<=j-COST_SHIP[START_ROOM];++k) { if(dp[START_ROOM][j]<dp[START_ROOM][j-k]+dp[v][k]) { dp[START_ROOM][j]=dp[START_ROOM][j-k]+dp[v][k]; } } } } }int main(){ while(cin>>NUM_ROOM>>NUM_SHIP) { if(NUM_ROOM==-1 && NUM_SHIP==-1)break; for(int i=0;i<NUM_ROOM;i++) { cin>>a[i].x>>POS_brain[i]; COST_SHIP[i]=(a[i].x+19)/20; } for(int i=0;i<NUM_ROOM;i++) { b[i].clear(); } for(int i=0;i<NUM_ROOM-1;i++) { cin>>START_ROOM>>GET_ROOM; b[START_ROOM-1].push_back(GET_ROOM-1); b[GET_ROOM-1].push_back(START_ROOM-1); } if(NUM_SHIP==0) { cout<<"0"<<endl; continue; } memset(dp,0,sizeof(dp)); dfs(0,-1); cout<<dp[0][NUM_SHIP]<<endl; } return 0;}
0 0
- HDOJ--1011
- HDOJ 1011 Starship Troopers
- HDOJ 1011 树形DP
- HDOJ 1011 Starship Troopers
- HDOJ
- hdoj
- hdoj
- HDOJ
- HDOJ-1011(树形DP)
- hdoj 1011 Starship Troopers(树状DP)
- HDOJ 1011 Starship Troopers(树形DP)
- hdoj 1568 && hdoj 5344 && hdoj 5444
- ACM-HDoj暑假竞赛(7)-1011解题报告
- poj 1011 hdoj 1455 Sticks(搜索+剪枝)
- [DFS+剪枝]POJ 1011/HDOJ 1455/HOJ 1049 Sticks
- HDOJ题目1011 Starship Troopers(树状dp)
- hdoj 1011 Starship Troopers 【树形DP + 01背包】
- HDOJ 2176
- 大师之路解惑基础概念篇:Tomcat与servlet联系与区别
- css reset归纳
- iOS开发 - 登陆post请求遇到的问题
- 一次服务器IO占用率高的定位分析
- 【JEECG技术博文】JEECG表单配置-树形表单
- HDOJ--1011
- Adapter局部刷新
- CSS浮动属性float史上最详细分析
- 去掉linux光标和制作linux Logo
- 文件断点续传和下载
- MySql Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 解决方法
- 一片不错的V4L2
- Kinect for Windows SDK开发学习相关资源
- Nginx