题意:

n个节点的树给出每个边的权值,有k个机器人,求由机器人走完所有节点的最小花费(所有机器人开始在根节点)

分析:

仔细看了几遍例题后,发现这个题的状态很巧妙,先从整体考虑,一个机器人走完所有边回到根,是所有边权值和的2倍,

对于k个机器人,有的就不用回根,dp[i][j],以i为根的子树,用机器人数j最多可以减少的花费,最后2*sum-dp[s][k]即为所求。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct tree{
int u,v,next,cost;
}t[];
int dp[][],n,k,s,used[],head[],len;
void add(int a,int b,int c){
t[len].u=a;
t[len].v=b;
t[len].cost=c;
t[len].next=head[a];
head[a]=len++;
}
void dfs(int root){
used[root]=;
for(int i=head[root];i!=-;i=t[i].next){
int son=t[i].v;
if(used[son])continue;
dfs(son);
for(int j=k;j>=;j--)
for(int l=;l<=j;++l)
dp[root][j]=max(dp[root][j],dp[root][j-l]+dp[son][l]+(-l)*t[i].cost);//l个机器人过这个边,减少的花费(2-l)*t[i].cost
}
}
int main()
{
while(~scanf("%d%d%d",&n,&s,&k)){
memset(dp,,sizeof(dp));
memset(used,,sizeof(used));
memset(head,-,sizeof(head));
int a,b,c,sum=;
len=;
for(int i=;i<n-;++i){
scanf("%d%d%d",&a,&b,&c);
sum+=c;
add(a,b,c);
add(b,a,c);
}
dfs(s);
printf("%d\n",*sum-dp[s][k]);
}
return ;
}

HDU 4003-Find Metal Mineral(树状背包)的更多相关文章

  1. hdu 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

  2. hdu 4003 Find Metal Mineral 树形dp ,*****

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  3. HDU 4003 Find Metal Mineral(分组背包+树形DP)

    题目链接 很棒的一个树形DP.学的太渣了. #include <cstdio> #include <string> #include <cstring> #incl ...

  4. HDU 4003 Find Metal Mineral (树形DP,经典)

    题意:给定一棵树图,n个节点,有边权,要派k<11个机器人从节点s出发,遍历所有的点,每当1只机器人经过1条边时就会花费该边的边权,边是可重复走的.问遍历完所有点的最小花费? 思路: 非常经典, ...

  5. HDU 4003 Find Metal Mineral

    这个题是POJ1849的加强版. 先说一个很重要的结论,下面两种方法都是从这个结论出发的. 一个人从起点遍历一颗树,如果最终要回到起点,走过的最小权值就是整棵树的权值的2倍. 而且K个人的情况也是如此 ...

  6. POJ 3345-Bribing FIPA(树状背包)

    题意: 有n个国家投票,要得到一个国家的投票有一定的花费,如果给到一个国家的票同时也得到了它所有附属国的票,给出国家关系树,求至少得到m票的最小花费. 分析:基础树状背包,dp[i][j],以i为根的 ...

  7. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  8. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  10. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

随机推荐

  1. 运行windows系统工具命令

    appwiz.cpl 卸载/安装程序  wscui.cpl 操作中心 inetcpl.cpl  查看Internet属性  eventvwr     查看监视消息和疑难解答消息  taskmgr  任 ...

  2. 在一个工程管理多个应用-b

    Demo:http://download.csdn.net/detail/u012881779/9166527 本文的产生是因产品经理提出的特殊需求: 一个针对多所学校的应用,对不同学校需要分别使用一 ...

  3. Java程序员的发展前景

    不知道什么时候开始,IT业初级程序员的工作性质与进城打工的"农民工"变得如此惊人的相似.很多IT公司的高管认为"人便宜,就是要用到坏掉,然后再找更便宜.更年轻的" ...

  4. poj 1679 The Unique MST(唯一的最小生成树)

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  5. bzoj 1222: [HNOI2001]产品加工 dp

    1222: [HNOI2001]产品加工 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 381  Solved: 218[Submit][Status ...

  6. Matlab中添加搜索目录

    一.问题来源 来自于一份大规模hash图像检索代码. 二.问题解析 2.1 添加目录 addpath('./utils/'); 2.2 添加目录及其子目录 addpath(genpath('./uti ...

  7. "Principles of Reactive Programming" 之<Actors are Distributed> (1)

    week7中的前两节课的标题是”Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka Clus ...

  8. <span> <div> 局部 keydown ,keyup事件。页面部分div $(document) 无效,可能焦点,添加焦点。

    前天改一个bug, js 实现的一个 面板拖拉,左右各两个列表,中间面板画线连接,页面左侧列表选中后,key 事件无效.右侧选中确有效,很奇怪,查看源码,左侧选中后,$(document).on(&q ...

  9. 【leetcode】Dungeon Game (middle)

    The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...

  10. Mybatis代码生成器 xml配置文件 连接SQL SERVER 2005

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguratio ...