“访问”美术馆
题目描述
经过数月的精心准备,Peer Brelstet,一个出了名的盗画者,准备开始他的下一个行动。艺术馆的结构,每条走廊要么分叉为两条走廊,要么通向一个展览室。Peer知道每个展室里藏画的数量,并且他精确测量了通过每条走廊的时间。由于经验老到,他拿下一幅画需要5秒的时间。你的任务是编一个程序,计算在警察赶来之前,他最多能偷到多少幅画。
输入格式
第1行是警察赶到的时间,以s为单位。第2行描述了艺术馆的结构,是一串非负整数,成对地出现:每一对的第一个数是走过一条走廊的时间,第2个数是它末端的藏画数量;如果第2个数是0,那么说明这条走廊分叉为两条另外的走廊。数据按照深度优先的次序给出,请看样例。
一个展室最多有20幅画。通过每个走廊的时间不超过20s。艺术馆最多有100个展室。警察赶到的时间在10min以内。
输出格式
输出偷到的画的数量
输入输出样例
输入 #1
60
7 0 8 0 3 1 14 2 10 0 12 4 6 2
输出 #1
2
【解题思路】
基础树形dp 有依赖性背包问题
f[i][j]表示当前节点为i用掉j秒所取得的最大值
转移的时候 如果当前节点是子节点,就判断能取多少
如果不是就枚举当前节点所分配给左树的时间,由左右子树的和转移来。
【code】
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define maxn 1001 5 using namespace std; 6 int n,cnt,tot; 7 int f[maxn][maxn]; 8 void dfs() 9 { 10 int root=++cnt,limit,time; 11 scanf("%d%d",&limit,&tot); 12 limit<<=1; 13 if(tot)//子节点 14 { 15 for(int time=limit;time<=n;time++) 16 f[root][time]=min((time-limit)/5,tot);//判断取多少 17 } 18 else 19 { 20 int left=cnt+1,right;dfs(); 21 right=cnt+1;dfs(); 22 for(int time=limit;time<=n;time++) 23 for(int lctime=0;lctime<=time-limit;lctime++)//分配给左树的时间 24 { 25 f[root][time]=max(f[root][time],f[left][lctime]+f[right][time-limit-lctime]);//左右子树的和 26 } 27 } 28 } 29 int main() 30 { 31 scanf("%d",&n);n--; 32 dfs(); 33 printf("%d\n",f[1][n]); 34 return 0; 35 }