题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4003

题目大意:有K个机器人,走完树上的全部路径,每条路径有个消费。对于一个点,机器人可以出去再回来,开销2倍。也可以不回来,一直停在某个点(如果你的机器人数量足够多的话)。问最小开销。

解题思路:

其实这题只能说是类树形背包。

用dp[i][j]表示在i点,有j个不回来的机器人走过的最小开销。

比如dp[i][0]就表示,i点及其子点全部靠其它点的不回来的机器人探索。所以机器人是一来一回开销2倍。

for(K...j...0) //j可以为0

dp[i][j]+=dp[t][0]+2*e[a].w;    //表示子点t全靠其它点的机器人探索

for(1..k....j)  //k要从1开始了,且k最大可以为j,也就是说父亲点可以是0个机器人,子点的机器人下去之后又回到子点,又去探索新的子点的子点。这样必然有父亲点0的情况。

dp[i][j]=min(dp[i][j],dp[i][j-k]+dp[t][k]+k*e.w); //权在边的计算模板

#include "cstdio"
#include "vector"
#include "iostream"
#include "cstring"
using namespace std;
#define maxn 10005
struct Edge
{
int to,next,w;
}e[maxn*];
int dp[maxn][],head[maxn],tol;
int n,s,K,u,v,w;
void addedge(int u,int v,int w)
{
e[tol].to=v;
e[tol].next=head[u];
e[tol].w=w;
head[u]=tol++;
}
void dfs(int root,int pre)
{
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=K;j>=;j--)
{
dp[i][j]+=dp[t][]+*e[a].w;
for(int k=;k<=j;k++)
dp[i][j]=min(dp[i][j],dp[i][j-k]+dp[t][k]+k*e[a].w);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&s,&K))
{
memset(dp,,sizeof(dp));
memset(head,-,sizeof(head));
tol=;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
dfs(s,s);
printf("%d\n",dp[s][K]);
}
}
2882723 neopenx HDU 4003 Accepted 1092 265 C++ 1072
2014-10-24 12:36:57

HDU 4003 (树形DP+背包)的更多相关文章

  1. hdu 1011 树形dp+背包

    题意:有n个房间结构可看成一棵树,有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个房间有一定的敌人,每个士兵可以对抗20个敌人,士兵在某个房间对抗敌人使无法走开,同时有一个价值,问你花费这m个士 ...

  2. hdu 4003 树形dp+分组背包 2011大连赛区网络赛C

    题意:求K个机器人从同一点出发,遍历所有点所需的最小花费 链接:点我 Sample Input 3 1 1 //3个点,从1出发,1个机器人 1 2 1 1 3 1 3 1 2 1 2 1 1 3 1 ...

  3. HDU 1561 (树形DP+背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1561 题目大意:从树根开始取点.最多取m个点,问最大价值. 解题思路: cost=1的树形背包. 有 ...

  4. HDU 1011 (树形DP+背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1011 题目大意:树上取点,先取父亲,再取儿子.每个点,权为w,花费为cost,给定m消费总额,求最大 ...

  5. hdu 4003(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 思路:dp[i][j]表示以i为根选择j个机器人的最小花费,然后就是背包了:dp[u][i]=m ...

  6. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

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

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

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

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

  9. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

随机推荐

  1. Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization

    原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  2. ASP注入靶机

     ASP:   <%  Dim Db,MyDbPath dim conn '可修改设置一:========================定义数据库类别,1为SQL数据库,0为Access数据库 ...

  3. [ruby on rails] 跟我学之(9)删除数据

    首先需要在index页加个删除链接,并提供一个删除的确认,用户确认删除时,直接删除数据. 修改views 修改 app/views/posts/index.html.erb,如下: <h1> ...

  4. HTML5/CSS3开发工具

    1.谷歌Google Web Designer https://www.google.com/webdesigner/ 这个工具不能在xp上运行,可以在win7 win8上运行 http://www. ...

  5. linux下用mii-tool和ethtool 查看网线是否正确连接到网卡

    输入mii-tool可以查看网线是否连接到网卡#mii-tool eth0: negotiated 100baseTx-FD, link ok 有时驱动可能不支持会出错下列错误#mii-tool SI ...

  6. 【leetcode】Subsets

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  7. iterator与const_iterator及const iterator区别

    如果你传递过来一个const类型的容器,那么只能用const_iterator来遍历.  C++ Code  12345   void Method(const vector<int> v ...

  8. 初识lua

    转自:http://www.oschina.net/question/12_115993-- 两个横线是单行注释(译者注:这跟 SQL 一样) --[[ 增加两个 [ 和 ] 变成多行注释 我是多行注 ...

  9. Java for LeetCode 142 Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. Java for LeetCode 035 Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...