题意:

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. flash链接需要后台调用时的插入flash方法

    <!--<textarea id="syslink" style="display:none;">// 1:打宝塔 2:山寨玩法 3:跨服竞技 ...

  2. HTML5 中的块级链接

    英文叫做 “Block-level” links,我以为只有我厂那些鸟毛不知道,没想到不知道的还挺多, 需要普及一下. 最近看了 kejun 的 PPT 前端开发理论热点面对面:从怎么看,到怎么做?, ...

  3. bzoj 3637: Query on a tree VI 树链剖分 && AC600

    3637: Query on a tree VI Time Limit: 8 Sec  Memory Limit: 1024 MBSubmit: 206  Solved: 38[Submit][Sta ...

  4. jmp && call && ret 特权级转移 & 进程调度

    ①jmp是不负责任的调度,不保存任何信息,不考虑会回头.跳过去就什么也不管了.②call,保存eip等,以便程序重新跳回.ret是call的逆过程,是回头的过程.这都是cpu固有指令,因此要保存的信息 ...

  5. javascript禁用与启用select标签(实用版)

    <html><head><script type="text/javascript">function disable()  {  docume ...

  6. Linux rm命令

    rm可以用来删除文件和文件夹.  rm --help Usage: rm [OPTION]... FILE... Remove (unlink) the FILE(s).   -f, --force ...

  7. linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法

    环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...

  8. define中的:#,##,#@

    [define中的:#,##,#@] #define Conn(x,y) x##y #define ToChar(x) #@x #define ToString(x) #x (2)x##y表示什么?表 ...

  9. SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)

    一.LDAP server在哪 By default, Spring Security’s LDAP authentication assumes that the LDAP server is li ...

  10. fd_set 用法

    http://www.cnblogs.com/wolflion/archive/2011/07/13/2539137.html select()函数主要是建立在fd_set类型的基础上的.fd_set ...