hdu1561 The more, The Better(常见分组背包+树形dp)
来源:互联网 发布:美女公寓小说txt淘宝 编辑:程序博客网 时间:2024/05/19 15:42
题意:中文题意。
思路:分析完后题意后,攻击先后顺序就是一个有向边,每条边权值1,这个城堡能直接攻击就把他和0点相连。那么这就是一个在树上分组背包的题目。
设:dp[i][j]表示在i点已经攻击了j个城堡的最大值。 dp[i][j]=max(dp[i][j-k]+dp[v][k]); v是i的子节点。
注意多了一个0点后,相当于多了一个城堡。
#include<bits/stdc++.h>using namespace std;template<int N,int M>//N点的个数,M边的个数struct Graph{ int top; struct Vertex{ int head; }V[N]; struct Edge{ int v,next; }E[M]; void init(){ memset(V,-1,sizeof(V)); top = 0; } void add_edge(int u,int v){ E[top].v = v; E[top].next = V[u].head; V[u].head = top++; }};const int maxn=200+10;Graph<maxn,maxn*2> g;int n,m,dp[maxn][maxn],val[maxn],vis[maxn];void dfs(int u){ dp[u][1]=val[u]; for(int i=g.V[u].head;i!=-1;i=g.E[i].next){ int v=g.E[i].v; dfs(v); for(int j=m+1;j>=1;j--){ for(int k=1;k<j;k++){ dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]); } } }}int main(){ while(~scanf("%d%d",&n,&m)&&n&&m){ g.init(); val[0]=0; for(int i=1;i<=n;i++){ int a,b;scanf("%d%d",&a,&val[i]); g.add_edge(a,i); } memset(dp,0,sizeof(dp)); dfs(0); printf("%d\n",dp[0][m+1]); }}
0 0
- hdu1561 The more, The Better(常见分组背包+树形dp)
- HDU1561 The more,The Better(树形DP+背包)
- hdu1561(树形dp+01背包) The more, The Better
- HDU1561:The more, The Better(树形dp & 01背包)
- HDU1561:The more, The Better(树形DP+01背包)
- HDU1561 The more,The better 树形DP
- HDU1561 树形DP The more, The Better
- HDU1561 The more, The Better(树形dp)
- [hdu1561] The more, The Better 【树形DP】
- 【树形DP】HDU1561-The more, The Better
- hdu1561 The more, The Better--树形dp
- hdu1561 the more , the better 树形dp
- HDU1561 The more, The Better(树形依赖背包)
- HDU1561 The more, The Better(树形DP)
- HDU1561 The more, The Better 解题报告【树上DP/背包】
- hdu 1561 The more, The Better(树形dp+分组背包)
- HDU 1561 The more, The Better(树形dp+背包)
- The more, The Better (树形dp+背包)
- Path 属性
- stat
- Faster R-CNN论文笔记——FR
- 第5课:HTML的注释
- js常见的2种形式
- hdu1561 The more, The Better(常见分组背包+树形dp)
- spring boot No qualifying bean of type [org.springframework.boot.autoconfigure.web.ErrorAttributes]
- AppCrawler使用教程(1)
- Common PHP Errors
- SQL Server数据库结构(MSSQL入门)
- 各种python 包的exe安装包下载地址
- 欢迎使用Markdown编辑器写博客
- shell中的wait
- TCP协议--CLOSE_WAIT状态