@xunuo
2017-03-04T12:50:34.000000Z
字数 3538
阅读 991
Time Limit: 1000MS Memory Limit: 65536K
树形DP
来源:
poj 2486 Apple Tree
vjudge A-Apple Tree
Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each node has an amount of apples. Wshxzt starts her happy trip at one node. She can eat up all the apples in the nodes she reaches. HX is a kind guy. He knows that eating too many can make the lovely girl become fat. So he doesn’t allow Wshxzt to go more than K steps in the tree. It costs one step when she goes from one node to another adjacent node. Wshxzt likes apple very much. So she wants to eat as many as she can. Can you tell how many apples she can eat in at most K steps.
There are several test cases in the input
Each test case contains three parts.
The first part is two numbers N K, whose meanings we have talked about just now. We denote the nodes by 1 2 ... N. Since it is a tree, each node can reach any other in only one route. (1<=N<=100, 0<=K<=200)
The second part contains N integers (All integers are nonnegative and not bigger than 1000). The ith number is the amount of apples in Node i.
The third part contains N-1 line. There are two numbers A,B in each line, meaning that Node A and Node B are adjacent.
Input will be ended by the end of file.
Note: Wshxzt starts at Node 1.
For each test case, output the maximal numbers of apples Wshxzt can eat at a line.
2 1
0 11
1 2
3 2
0 1 2
1 2
1 3
11
2
题意:
一个叔叔给了一个小女孩一颗苹果树,但是又怕她吃胖了,所以呢就规定小女孩吃苹果的时候只能走k步,而小女孩非常喜欢吃苹果,因此就想要在k步之内吃到最多的苹果,hope you can help her......
解题思路:
解题思路...这个东西我并没有(ಥ _ ಥ)
好难啊啊啊啊啊啊啊啊!!!!!!
它的有些东西,到现在都看不懂......(○´・д・)ノ
他状态的转换是这样的:
如果你从 一棵数的顶点向它的某一棵子树v出发,有三种结果:
1、返回顶点;
2、留在v子树;
3、留在其他子树;
故,设DP[u][k]0/1为在u节点为根的子树上走k步并且不回/回到u的最大权值,则它的状态方程为:
1、dp[u][i][1]=max(dp[u][i][1],dp[v][j-2][1]+dp[u][i-j][1]); //最后要返回顶点
2、 dp[u][j][0]=max(dp[u][j][0],dp[v][w-1][0]+dp[u][j-w][1]);//留在该子树,故要先把其他的子树跑完了才去v子树
3、dp[u][j][0]=max(dp[u][j][0],dp[v][w-2][1]+dp[u][j-w][0]);//;留在其他子树,要先跑完v,回去之后才去跑其他的子树
完整代码--------(用邻接表和vector都敲了一次,然而还是不明白)
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
int num[210],n,k;
int dp[210][410][2];///0表示不回,1表示回
int head[210];
struct node
{
int u,v,val,next;
}tree[510];
int l;
///***///
void add(int a,int b)
{
tree[l].u=a;
tree[l].v=b;
tree[l].next=head[a];
head[a]=l;
l++;
}
void dfs(int u,int p)
{
for(int i=head[u];i!=-1;i=tree[i].next)
{
int v=tree[i].v;
if(v==p)
continue;
dfs(v,u);
for(int j=k;j>0;j--)
for(int w=1;w<=j;w++)
{
dp[u][j][0]=max(dp[u][j][0],dp[v][w-1][0]+dp[u][j-w][1]);
dp[u][j][0]=max(dp[u][j][0],dp[v][w-2][1]+dp[u][j-w][0]);
dp[u][j][1]=max(dp[u][j][1],dp[v][w-2][1]+dp[u][j-w][1]);
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(num,0,sizeof(num));
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
for(int j=0;j<=k;j++)
dp[i][j][0]=dp[i][j][1]=num[i];
}
int a,b;
l=0;
for(int i=1;i<n;i++)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
dfs(1,0);
int ans=max(dp[1][k][0],dp[1][k][1]);
printf("%d\n",ans);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<algorithm>
using namespace std;
int dp[210][210][2];///0表示不回,1表示回
int num[210],n,k;
vector<int>g[210];
void dfs(int u,int p)
{
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];///
if(v==p)
continue;
dfs(v,u);
for(int j=k;j>0;j--)
for(int w=1;w<=j;w++)
{
dp[u][j][0]=max(dp[u][j][0],dp[v][w-1][0]+dp[u][j-w][1]);
dp[u][j][0]=max(dp[u][j][0],dp[v][w-2][1]+dp[u][j-w][0]);
dp[u][j][1]=max(dp[u][j][1],dp[v][w-2][1]+dp[u][j-w][1]);
}
}
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i]);
for(int j=0;j<=k;j++)
dp[i][j][0]=dp[i][j][1]=num[i];
g[i].clear();
}
int a,b;
for(int i=1;i<n;i++)
{
scanf("%d%d",&a,&b);
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1,0);
int ans=max(dp[1][k][0],dp[1][k][1]);
printf("%d\n",ans);
}
return 0;
}