Find Metal Mineral

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

Hint

In the first case: 1->2->1->3 the cost is 3;
In the second case: 1->2; 1->3 the cost is 2;

 

题意:

题解:

我们设dp[i][j] 表示已i为根节点,放j个机器人的最小话费

那么:

dp(i,j)=(dp(son,0)+cast[son]*2)(子树没有停留机器人)+min(dp[p][m-y]+y*cost[son]+dp[son][y])(子树停留了y个机器人)

//meek
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; vector< pair<int ,int> >G[N];
int dp[N][],n,S,K,u,v,w;
void dfs(int x,int pre) {
for(int i=;i<G[x].size();i++) {
if(G[x][i].fi==pre) continue;
int son=G[x][i].fi,cost=G[x][i].se;
dfs(G[x][i].fi,x);
for(int k=K;k>=;k--) {
dp[x][k]+=dp[son][]+cost*;
for(int y=;y<=k;y++) {
dp[x][k]=min(dp[x][k],dp[x][k-y]+dp[son][y]+cost*y);
}
}
}
}
void init() { for(int i=;i<=n;i++) G[i].clear();
mem(dp);
}
int main() {
while(~(scanf("%d%d%d",&n,&S,&K))) {
init();
for(int i=;i<n;i+=) {
scanf("%d%d%d",&u,&v,&w);
G[u].pb(MP(v,w));
G[v].pb(MP(u,w));
}
dfs(S,-);
cout<<dp[S][K]<<endl;
} return ;
}

daima

HDU4003 Find Metal Mineral 树形DP的更多相关文章

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

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

  2. HDU4003Find 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

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

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

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

  5. hdu4003Find Metal Mineral(树形DP)

    4003 思维啊 dp[i][j]表示当前I节点停留了j个机器人 那么它与父亲的关系就有了 那条边就走了j遍 dp[i][j] = min(dp[i][j],dp[child][g]+dp[i][j- ...

  6. HDU4003 Find Metal Mineral

    看别人思路的 树形分组背包. 题意:给出结点数n,起点s,机器人数k,然后n-1行给出相互连接的两个点,还有这条路线的价值,要求最小花费 思路:这是我从别人博客里找到的解释,因为很详细就引用了 dp[ ...

  7. hdu4003详解(树形dp+多组背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Find Metal Mineral Time Limit: 2000/1000 MS (Jav ...

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

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

  9. 【树形dp】Find Metal Mineral

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

随机推荐

  1. centos下各种c++库文件的安装

    Centos编译boost   1.下载最新的boost http://www.boost.org/   2.解压文件 tar -xzvf boost_1_45_0.tar.gz    3.编译bja ...

  2. EF之高级查询

    首先我们来看看一个页面 这里面有多选的条件,大于,小于等等,包括每个字段都有 如此多的查询条件,我们的后台该如何实现呢? 难道我们还得每个参数都去判断吗? 那得传多少参数进来才能实现这个页面的功能啊! ...

  3. [shell练习]——awk练习题

    1. sed和awk有什么区别? (1)awk:按列(域)操作:sed:按行操作(2)awk:文本处理语言,适合对文本进行抽取处理:sed:非交互式的编辑器,适合对文本进行编辑 2. awk要处理域的 ...

  4. loadView, viewDidLoad 快速使用

    一  loadView: 在每次访问 UIViewController时,且其 view = nil 时,会调用这个方法,所以大家在开发中想自己设置 view 的可以用这个方法,在这个方法中自定义 v ...

  5. 微软职位内部推荐-SDE II

    微软近期Open的职位: Senior Software Development Engineer Job Title: Senior Development Engineer Division: V ...

  6. COS中访问文件的三种方式

    1.通过FID来访问文件(比如EF,DF) 2.通过SFI来访问文件(有些COS命令可以通过SFI来快速访问文件,而不需要事先选中文件) 3.通过文件名来访问文件(只能是DF文件)

  7. Log4j的常见用法

    对于log4j,一般常用的只要保存如下的内容为属性文件即可: #log4j.rootLogger=DEBUG,A1,R log4j.rootLogger=INFO,A1,R log4j.appende ...

  8. WPF中的Style(风格,样式)(转)

    在WPF中我们可以使用Style来设置控件的某些属性值,并使该设置影响到指定范围内的所有该类控件或影响指定的某一控件,比如说我们想将窗口中的所有按钮都保持某一种风格,那么我们可以设置一个Style,而 ...

  9. POJ2676-Sudoku(数独)

    想了好久没想到好的解决办法,参考了 http://user.qzone.qq.com/289065406/blog/1303713313 大致题意: 九宫格问题,也有人叫数独问题 把一个9行9列的网格 ...

  10. 使用HTML5中postMessage实现Ajax中的POST跨域问题

    HTML5中提供了在网页文档之间相互接收与发送信息的功能.使用这个功能,只要获取到网页所在窗口对象的实例,不仅仅同源(域+端口号)的web网页之间可以互相通信,甚至可以实现跨域通信. 浏览器支持程度: ...