ZOJ 3201 Tree of Tree 大水树形背包
来源:互联网 发布:编程工作怎么样 编辑:程序博客网 时间:2024/05/16 11:38
题意与做法:题目也不长,就是原始的树形背包,不过粗心了,敲代码的时候卡了一下。。
#include<stdio.h>#include<string.h>#define LMT 105int get[LMT][LMT],dp[LMT][LMT],wei[LMT],next[LMT];typedef struct{ int u,v,next;}line;line e[LMT<<1];int n,st,all;int max(int a,int b){ return a>b?a:b;}void insert(int u,int v){ e[all].u=u; e[all].v=v; e[all].next=next[u]; next[u]=all++; e[all].u=v; e[all].v=u; e[all].next=next[v]; next[v]=all++;}void dfs(int u,int pre){ int v,x,j,k; for(x=next[u];x!=-1;x=e[x].next) if(e[x].v!=pre) { v=e[x].v; dfs(v,u); for(j=st;j>=0;j--) for(k=0;k<=j;k++) if(get[u][j-k]!=-1) get[u][j]=max(get[u][j],get[u][j-k]+dp[v][k]); } dp[u][1]=wei[u]; for(j=st;j>=1;j--) for(k=0;k<=j-1;k++)//k竟然写成了j。。 if(dp[u][j-k]!=-1) dp[u][j]=max(dp[u][j],dp[u][j-k]+get[u][k]);}int main(){ int i; while(scanf("%d%d",&n,&st)!=EOF) { for(i=0;i<n;i++) scanf("%d",&wei[i]); memset(dp,-1,sizeof(dp)); memset(get,-1,sizeof(get)); memset(next,-1,sizeof(next)); all=0; for(i=0;i<n;i++) get[i][0]=dp[i][0]=0; for(i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); insert(u,v); } dfs(0,0); int ans=0;//忘记赋初值了。。。 for(i=0;i<n;i++) ans=max(ans,dp[i][st]); printf("%d\n",ans); } return 0;}
- ZOJ 3201 Tree of Tree 大水树形背包
- ZOJ 3201 Tree of Tree(树形背包DP)
- zoj 3201 Tree of Tree(树形背包dp)
- ZOJ 3201 Tree of Tree(树形dp + 分组背包)
- ZOJ 3201 Tree of Tree(树形DP+背包)
- ZOJ 3201 Tree of Tree 树形DP
- ZOJ - 3201 Tree of Tree 树形DP
- zoj 3201 Tree of Tree 树形dp基础题——树形背包
- zoj 3201 简单树形dp Tree of Tree
- ZOJ 3201 Tree of Tree(树形DP)
- TOJ 2676 ZOJ 3201 Tree of Tree / 树形DP
- 【树形dp】ZOJ-3326-Tree of Tree
- ZOJ 3201 Tree of Tree
- ZOJ 3201 Tree of Tree
- ZOJ - 3201 Tree of Tree
- Zoj 3201 Tree of Tree
- ZOJ 3201 Tree of Tree
- ZOJ - 3201 Tree of Tree
- SharePoint Pages(4)之应用程序页
- DWR单向推送
- SharePoint Pages(5)之应用程序页的安全性
- strtol函数
- java小小知识点
- ZOJ 3201 Tree of Tree 大水树形背包
- linux系统移植问题总结
- 如何在SharePoint中创建Custom Master Page
- 数字集成电路设计-3-除法器的verilog简单实现(续)
- 为SharePoint网站创建自定义导航菜单
- 用T-SQL操作面试SQL Server开发人员
- platform 驱动 failed to claim resource 2|3|4|5 错误
- (搜狗两道笔试题)子数组乘积|等概率发生器
- jquery 弹出div 屏幕正中间