wikioi 1163 訪问艺术馆 树形dp

递归建树,由题知该树是一棵二叉树,且除根节点外其它点的度为0或2。

dp[i][j]表示来到第i个走廊(还未走过这条走廊)还剩下j时间,能拿到最大的画的数量。

dp[i][j]=max(dp[i][j],dp[lson[i]][k]+dp[rson][last_time-k])

#include<cstdio>
#include<algorithm>
using namespace std;
int dp[200][700];
int id=0,x,y,s;
struct node
{
int t,l,r;
int v;
}tree[200];
void init(int now)
{
scanf("%d%d",&x,&y);
tree[now].t=x*2;
tree[now].v=y;
if(y!=0) tree[id].l=tree[id].r=-1;
else
{
tree[now].l=++id;init(id);
tree[now].r=++id;init(id);
}
}
int dfs(int now,int times)
{
if(dp[now][times]) return dp[now][times]; //记忆化
int last=times-tree[now].t;
if(last<0) return 0;
if(tree[now].l==-1) return dp[now][times]=min(tree[now].v,last/5);
int maxn=0;
for(int i=0;i<=last;i++)
{
maxn=max(maxn,dfs(tree[now].l,i)+dfs(tree[now].r,last-i)); /*这里会调用多次已经计算过的值*/
//计算左儿子i时间,右儿子last-i时间的最大偷画数
}
return dp[now][times]=maxn;
}
int main()
{
scanf("%d",&s);
init(0);
printf("%d\n",dfs(0,s));
return 0;
}

wikioi 1163 訪问艺术馆 树形dp

时间: 2024-07-30 22:30:28

wikioi 1163 訪问艺术馆 树形dp的相关文章

wikioi 1163 访问艺术馆 树形dp

递归建树,由题知该树是一棵二叉树,且除根节点外其他点的度为0或2. dp[i][j]表示来到第i个走廊(还未走过这条走廊)还剩下j时间,能拿到最大的画的数量. dp[i][j]=max(dp[i][j],dp[lson[i]][k]+dp[rson][last_time-k]) #include<cstdio> #include<algorithm> using namespace std; int dp[200][700]; int id=0,x,y,s; struct node

codevs1163访问艺术馆(树形dp)

1163 访问艺术馆 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 皮尔是一个出了名的盗画者,他经过数月的精心准备,打算到艺术馆盗画.艺术馆的结构,每条走廊要么分叉为二条走廊,要么通向一个展览室.皮尔知道每个展室里藏画的数量,并且他精确地测量了通过每条走廊的时间,由于经验老道,他拿下一副画需要5秒的时间.你的任务是设计一个程序,计算在警察赶来之前(警察到达时皮尔回到了入口也算),他最多能偷到多少幅画. 输入描述 Input

[DP总结]树形DP

树形DP 树形DP,顾名思义,是在树上进行DP操作,因此往往需要使用DP实现,在转移时,通常先递归求出子树中的解,再在根节点进行计算转移. 树中只有两种关系,一种是父子关系,一种是平行关系. 下面是几道简单的树形DP.从中我们可以窥出树形DP的本质. [luogu] P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员都会增加一定的快乐指数Ri,但是

CF 219D Choosing Capital for Treeland 树形DP 好题

一个国家,有n座城市,编号为1~n,有n-1条有向边 如果不考虑边的有向性,这n个城市刚好构成一棵树 现在国王要在这n个城市中选择一个作为首都 要求:从首都可以到达这个国家的任何一个城市(边是有向的) 所以一个城市作为首都,可能会有若干边需要改变方向 现在问,选择哪些城市作为首都,需要改变方向的边最少. 输出最少需要改变方向的边数 输出可以作为首都的编号 树形DP 先假定城市1作为首都 令tree(i)表示以i为根的子树 dp[i]表示在tree(i)中,若以i为首都的话,需要改变的边数 第一次

Codeforces 462D Appleman and Tree 树形dp

题目链接:点击打开链接 题意: 给定n个点的树, 0为根,下面n-1行表示每个点的父节点 最后一行n个数 表示每个点的颜色,0为白色,1为黑色. 把树分成若干个联通块使得每个联通块有且仅有一个黑点,问有多少种分法(结果mod1e9+7) 思路: 树形dp,每个点有2个状态,已经归属于某个黑点和未归属于某个黑点. #include <cstdio> #include <vector> #include <iostream> using namespace std; #de

Vijos 1523 贪吃的九头龙 【树形DP】

贪吃的九头龙 背景 安徽省芜湖市第二十七中学测试题 NOI 2002 贪吃的九头龙(dragon) Description:OfficialData:OfficialProgram:Converted by JackDavid127 描述 传说中的九头龙是一种特别贪吃的动物.虽然名字叫"九头龙",但这只是说它出生的时候有九个头,而在成长的过程中,它有时会长出很多的新头,头的总数会远大于九,当然也会有旧头因衰老而自己脱落. 有一天,有M个脑袋的九头龙看到一棵长有N个果子的果树,喜出望外,

poj 2342(树形DP)

Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6062   Accepted: 3490 Description There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure

CF 337D Book of Evil 树形DP 好题

Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some p

hdu4118 树形dp

http://acm.hdu.edu.cn/showproblem.php?pid=4118 Problem Description Nowadays, people have many ways to save money on accommodation when they are on vacation. One of these ways is exchanging houses with other people. Here is a group of N people who wan