题目链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626

题目大意:树中取点。每过一条边有一定cost,且最后要回到起点。给定预算m,问最大价值。

解题思路

首先要注意这题要回到起点,由于树的特殊结构(每个结点只有一个父亲)也就是说,要回到开头,

开销是2倍。所以首先m/=2。

然后就是树形背包的求解,这题的cost在边,所以for写法变成如下:

for(m....j....0)
     for(0....k....j-e.cost)

dp[i][j]=max(dp[i][j],dp[i][j-k-e.cost]+dp[t][k]);

for循环的主要变化是0的出现,也就是说某些点的开销可以是0(在父亲上算过了)。

所以初始化也要变成:for(int i=0;i<=m;i++) dp[root][i]=w[root];

DP方程的主要变化就是dp[i][j-k]   ->  dp[i][j-k-e.cost],这里之所以要减去e.cost,是为了防止cost的重复计算。

不妨设k=j-e.cost,你就会发现在计算dp[i][0],这也是为什么要推cost=0这个状态。

#include "cstdio"
#include "iostream"
#include "cstring"
using namespace std;
#define maxn 300
struct Edge
{
int to,next,c;
}e[maxn*];
int w[maxn],num[maxn],dp[maxn][maxn],head[maxn],tol;
int n,m,k,u,v,c;
void addedge(int u,int v,int c)
{
e[tol].to=v;
e[tol].next=head[u];
e[tol].c=c;
head[u]=tol++;
}
void dfs(int root,int pre)
{
for(int i=;i<=m;i++) dp[root][i]=w[root];
int i=root;
for(int a=head[root];a!=-;a=e[a].next)
{
int t=e[a].to;
if(t==pre) continue;
dfs(t,root);
for(int j=m;j>=;j--)
for(int k=;k<=j-e[a].c;k++)
dp[i][j]=max(dp[i][j],dp[i][j-k-e[a].c]+dp[t][k]);
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n))
{
memset(head,-,sizeof(head));
memset(dp,,sizeof(dp));
tol=;
for(int i=;i<=n;i++)
scanf("%d",&w[i]);
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
addedge(v,u,c);
}
scanf("%d%d",&k,&m);
m/=;
dfs(k,k);
printf("%d\n",dp[k][m]);
}
}
2875311 neopenx ZOJ 3626 Accepted 636 0 C++ (g++ 4.4.5) 1174
2014-10-22 09:13:13

ZOJ 3626(树形DP+背包+边cost)的更多相关文章

  1. ZOJ 3201 树形dp+背包(简单题)

    #include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...

  2. URAL_1018 Binary Apple Tree 树形DP+背包

    这个题目给定一棵树,以及树的每个树枝的苹果数量,要求在保留K个树枝的情况下最多能保留多少个苹果 一看就觉得是个树形DP,然后想出 dp[i][j]来表示第i个节点保留j个树枝的最大苹果数,但是在树形过 ...

  3. Vijos 1180 (树形DP+背包)

    题目链接: https://vijos.org/p/1180 题目大意:选课.只有根课选了才能选子课,给定选课数m, 问最大学分多少. 解题思路: 树形背包.cost=1. 且有个虚根0,取这个虚根也 ...

  4. BZOJ.1017.[JSOI2008]魔兽地图(树形DP 背包DP)

    题目链接 树形DP,考虑子节点对父节点的贡献. 设f[x][i][j]表示当前为x,用i个x去合成上一层装备,花费为j的最大价值. 由子节点转移时 是一个分组背包,需要一个辅助数组g[i][j]表示前 ...

  5. hdu1561 The more, The Better (树形dp+背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...

  6. 【BZOJ-1017】魔兽地图DotR 树形DP + 背包

    1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1566  Solved: 705[Submit][S ...

  7. codeforces 212E IT Restaurants(树形dp+背包思想)

    题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...

  8. 树形DP+背包(poj1155泛化分组背包)

    TELE Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3675   Accepted: 1936 Description ...

  9. joyOI 选课 【树形dp + 背包dp】

    题目链接 选课 题解 基础背包树形dp #include<iostream> #include<cstdio> #include<cmath> #include&l ...

随机推荐

  1. why we use Symbols in Hash

    Rather than using Strings as the keys in a Hash, it’s better practice to use Symbols. Symbols are ju ...

  2. BestCoder Round #60 1001

    Problem Description You are given a sequence of NNN integers. You should choose some numbers(at leas ...

  3. ubuntu硬盘安装卡在探测文件系统

    在硬盘安装ubuntu的时候,会出现这样的问题:安装程序一直卡在正在探测文件系统就不动了.解决的方法很简单.在安装之前要在终端输入sudo空格umount空格 -l空格 /isodevice 不能少一 ...

  4. luarocks install with lua5.1 and luajit to install lapis

    # in luarocks source directory...git clone https://github.com/archoncap/luarockscd luarocks ./config ...

  5. microsoft office安装选择

    office分为零售版和批量授权版 零售版(文件名以cn开头)需要提供序列号才可以安装,而批量授权版(文件名以SW开头)可以先安装试用一段时间.

  6. SCOPE_IDENTITY的作用

    SCOPE_IDENTITY返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值.一个作用域就是一个模块——存储过程.触发器.函数或批处理.因此,如果两个语句处于同一个存储 ...

  7. Python之property装饰器

    参考: http://www.cnblogs.com/lovemo1314/archive/2011/05/03/2035600.html http://joy2everyone.iteye.com/ ...

  8. powerdesigner奇淫技

    在日常开发中数据库的设计常常需要建立模型,而powerdesigner是个不错的选择.但很多时候用powerdesigner生成模型后再去创建表结构,会觉得烦和别扭.那么能不能数据库表建好后再生成模型 ...

  9. PHP 调试用函数

    2014年7月4日 10:27:59 有些系统函数可以在调试程序时救急用: get_class_methods(); get_class_vars(); get_object_vars(); get_ ...

  10. useradd mfs -s /sbin/nologin -M

    创建用户但不建家目录