首页 > 技术文章 > codevs1163访问艺术馆(树形dp)

L-Memory 2017-02-02 11:25 原文

1163 访问艺术馆

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
题目描述 Description

    皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画。艺术馆的结构,每条走廊要么分叉为二条走廊,要么通向一个展览室。皮尔知道每个展室里藏画的数量,并且他精确地测量了通过每条走廊的时间,由于经验老道,他拿下一副画需要5秒的时间。你的任务是设计一个程序,计算在警察赶来之前(警察到达时皮尔回到了入口也算),他最多能偷到多少幅画。

输入描述 Input Description

第1行是警察赶到得时间,以s为单位。第2行描述了艺术馆得结构,是一串非负整数,成对地出现:每一对得第一个数是走过一条走廊得时间,第2个数是它末端得藏画数量;如果第2个数是0,那么说明这条走廊分叉为两条另外得走廊。数据按照深度优先得次序给出,请看样例

输出描述 Output Description

输出偷到得画得数量

样例输入 Sample Input
 
 

60

7 0 8 0 3 1 14 2 10 0 12 4 6 2

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

s<=600

走廊的数目<=100

 

/*
基础树形dp   有依赖性背包问题
f[i][j]表示当前节点为i用掉j秒所取得的最大值
转移的时候 如果当前节点是子节点,就判断能取多少
如果不是就枚举当前节点所分配给左树的时间,由左右子树的和转移来。 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1001

using namespace std;
int n,cnt,tot;
int f[maxn][maxn];

void dfs()
{
    int root=++cnt,limit,time;
    scanf("%d%d",&limit,&tot);
    limit<<=1;
    if(tot)//子节点 
    {
        for(int time=limit;time<=n;time++)
          f[root][time]=min((time-limit)/5,tot);//判断取多少 
    }
    else
    {
        int left=cnt+1,right;dfs();
        right=cnt+1;dfs();
        for(int time=limit;time<=n;time++)
          for(int lctime=0;lctime<=time-limit;lctime++)//分配给左树的时间 
          {
              f[root][time]=max(f[root][time],f[left][lctime]+f[right][time-limit-lctime]);//左右子树的和 
          }
    }
}

int main()
{
    scanf("%d",&n);
    dfs();
    printf("%d\n",f[1][n]);
    return 0;
}
心若向阳,无谓悲伤

 注意:全局变量不一定是好东西......坑死我了!!!

推荐阅读