看别人思路的

树形分组背包。

题意:给出结点数n,起点s,机器人数k,然后n-1行给出相互连接的两个点,还有这条路线的价值,要求最小花费

思路:这是我从别人博客里找到的解释,因为很详细就引用了

dp[i][j]表示对于以i结点为根结点的子树,放j个机器人所需要的权值和。

当j=0时表示放了一个机器人下去,遍历完结点后又回到i结点了。状态转移方程类似背包

如果最终的状态中以i为根结点的树中有j(j>0)个机器人,那么不可能有别的机器人r到了这棵树后又跑到别的树中去

因为那样的话,一定会比j中的某一个到达i后跑与r相同的路径再回到i,再接着跑它的路径要差(多了一条i回去的边)

这样的话,如果最后以i为根结点的树中没有机器人,那么只可能是派一个机器人下去遍历完后再回来

可以这么理解:

对于每个根节点root,有个容量为K的背包

如果它有i个儿子,那么就有i组物品,价值分别为dp[son][0],dp[son][1].....dp[son][k] ,这些物品的重量分别为0,1,.....k

现在要求从每组里选一个物品(且必须选一个物品)装进root的背包,使得容量不超过k的情况下价值最大。

那么这就是个分组背包的问题了。

但是这里有一个问题,就是每组必须选一个物品。

对于这个的处理,我们先将dp[son][0]放进背包,如果该组里有更好的选择,那么就会换掉这个物品,否则的话这个物品就是最好的选择。这样保证每组必定选了一个。

Find Metal Mineral

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 2198    Accepted Submission(s): 1001

Problem Description
Humans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on Mars to collect them, and due to the unknown reasons, the landing site S of all robots is identified in advanced, in other word, all robot should start their job at point S. Each robot can return to Earth anywhere, and of course they cannot go back to Mars. We have research the information of all paths on Mars, including its two endpoints x, y and energy cost w. To reduce the total energy cost, we should make a optimal plan which cost minimal energy cost.
 
Input
There are multiple cases in the input. In each case: The first line specifies three integers N, S, K specifying the numbers of metal mineral, landing site and the number of robots. The next n‐1 lines will give three integers x, y, w in each line specifying there is a path connected point x and y which should cost w. 1<=N<=10000, 1<=S<=N, 1<=k<=10, 1<=x, y<=N, 1<=w<=10000.
 
Output
For each cases output one line with the minimal energy cost.
 
Sample Input
3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
 
Sample Output
3
2
#include <stdio.h>
#include<iostream>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int now,next,val;
}tree[20005];
int len,n,s,k;
int head[10005],dp[10005][15],vis[10005];
void add(int x,int y,int v)
{
tree[len].now=y;
tree[len].val=v;
tree[len].next=head[x];
head[x]=len++;
}
void dfs(int root,int p)
{
int i,j,q,son;
vis[root]=1;
for(i=head[root];i!=-1;i=tree[i].next)
{
son=tree[i].now;
if(son==p)
continue;
if(!vis[son])
{
dfs(son,root);
for(j=k;j>=0;j--)
{
dp[root][j]+=dp[son][0]+2*tree[i].val;//先将dp[son][0]放进背包,
//由于dp[son][0]是表示用一个机器人去走完所有子树,
//最后又回到pos这个节点,所以花费要乘以2
for(q=1;q<=j;q++)//在这里找到比dp[son][0]更优的方案,分组背包
dp[root][j]=min(dp[root][j],dp[root][j-q]+dp[son][q]+q*tree[i].val);
//必须是乘以p,p以前的价值要加上。
}
}
}
} int main()
{
int i,x,y,w;
while(~scanf("%d%d%d",&n,&s,&k))
{
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
len=0;
for(i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
dfs(s,-1);
printf("%d\n",dp[s][k]);
}
return 0;
}

HDU4003 Find Metal Mineral的更多相关文章

  1. HDU4003 Find Metal Mineral 树形DP

    Find Metal Mineral Problem Description Humans have discovered a kind of new metal mineral on Mars wh ...

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

    题目大意:用m个机器人去遍历有n个节点的有根树,边权代表一个机器人通过这条边的代价,求最小代价. 题目分析:定义状态dp(root,k)表示最终遍历完成后以root为根节点的子树中有k个机器人时产生的 ...

  3. HDU-4003 Find Metal Mineral 树形DP (好题)

    题意:给出n个点的一棵树,有k个机器人,机器人从根节点rt出发,问访问完整棵树(每个点至少访问一次)的最小代价(即所有机器人路程总和),机器人可以在任何点停下. 解法:这道题还是比较明显的能看出来是树 ...

  4. 【树形dp】Find Metal Mineral

    [HDU4003]Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (J ...

  5. HDU4003Find Metal Mineral[树形DP 分组背包]

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

  6. hdu 4003 Find Metal Mineral 树形DP

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

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

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

  8. 树形DP-----HDU4003 Find Metal Mineral

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

  9. HDOJ 4003 Find Metal Mineral

    题意: 一棵有权树,从根结点中放入 K 个机器人.求用这 K 个机器人遍历全部的结点最少的权值和. 思路: 1. dp[u][i] 表示给以 u 为根节点的子树放 i 个机器人,遍历其子树所须要的最小 ...

随机推荐

  1. (转)javaScript call 函数的用法说明

    call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, [,.argN ...

  2. 一次利用MSSQL的SA账户提权获取服务器权限

    遇到小人,把服务器整走了 自己手里只有sql server的sa账号密码 模糊记起之前用这个账户提权读取文件的事 百度之,发现相关信息一堆堆 各种工具也用了不少 发现不是语法错误就是权限不够 无奈之下 ...

  3. C# 里窗体里(windows form)怎么播放音乐

    在.NET的winform里面,没有托管的音乐播放器,API只能播放WAV格式,对于MP3等形式的音频文件,就要依赖于 MediaPlayer里,嘿嘿 使用的方法: 在toolbox上点右键,选择“选 ...

  4. MVC5+EF6 入门完整教程一

    第0课 从0开始 ASP.NET MVC开发模式和传统的WebForm开发模式相比,增加了很多"约定". 直接讲这些 "约定" 会让人困惑,而且东西太多容易忘记 ...

  5. jQuery实现页面元素智能定位

    实现过程 Js侦听滚动事件,当页面滚动的距离(页面滚动的高度)超出了对象(要滚动的层)距离页面顶部的高度,即要滚动的层到达了浏览器窗口上边缘时,立即将对象定位属性position值改成fixed(固定 ...

  6. 查看Jquery版本

    1. $.fn.jquery > "1.11.1" 2. 通过这样可以 判断一个对象是否是jquery对象!!

  7. 利用CMake自己创建OpenCV静态链接库

    1.准备工作: 1)完成Visual Studio2012安装: 2)下载并解压CMake3.5.0: 3)下载并解压OpenCV2.4.12: 4)下载并解压TBB44_20160128oss. 2 ...

  8. python sklearn模型的保存

    使用python的机器学习包sklearn的时候,如果训练集是固定的,我们往往想要将一次训练的模型结果保存起来,以便下一次使用,这样能够避免每次运行时都要重新训练模型时的麻烦. 在python里面,有 ...

  9. php中的JSON中文处理

    最近在PHP中要输出JSON,上网查了一下,对中文支持不太好,要不就先转成utf-8的编码,再用json_encode生成,客户端还要再utf-8转中文.对于网页已经用GB2312的服务器,不想这样折 ...

  10. 封装Timer

    System.Timers.Timer,System.Timers.Timer在使用的过程中需要: 1.构造函数不同,构造函数可以什么事情也不做,也可以传入响应间隔时间:System.Timers.T ...