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. MySQL使用rand函数实现随机数[转]

    如何写一个语句能一下更新几百条MYSQL数据! 需要测试MYSQL数据库,里面有一个上万条数据的数据库,如何写一个PHP文件一下每次更新几百条信息,我都是写一个循环一次更新一条信息,这样我知道用WHI ...

  2. 分享C#原生ID(流水号)生成功能实现

    ///, , )).TotalMilliseconds.ToString(")); /// <summary>         ///         /// </summ ...

  3. Swift 1.2 正式发布 - 带来很多重大改进

    Swift 1.2 随着 Xcode 6.3 Beta 正式发布了.这次的 beta 发布包含了对 Swift 编译器显著的改进.还有对 Swift 语言本身的新特性的增加.这篇文章介绍下主要部分. ...

  4. json转换对象

    JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串

  5. 我的VS2013中,用Ado.net给SQLParameter赋值的时候,当赋值null的时候,生成的sql语句是default

    /// <summary> /// 增加一条数据 /// </summary> public bool Add(Model.WechatDocuments model) { S ...

  6. VRP-Lua学习笔记

    至于vrp是什么东西以及为什么要学习vrp,vrp的简单操作这些问题请自行右转抵拢倒拐找百度或者去中视典官网去找教程,我这里不会在赘述. 今天默认我们已经会使用VRP的脚本编辑器,用其他语言来为VRP ...

  7. DebugViewHierarchy

    DebugViewHierarchy(视图调试)是XCode6新出的一项功能,它可以让开发者在程序运行时,动态的查看当前界面的显示情况,包括视图的层次,控件的大小和位置,而且会以3D效果显示当前视图的 ...

  8. PBOC2.0与3.0的区别

    一.PBOC规范颁布的历程 1997年12月,PBOC V1.0  定义了五个方面的事项  电子钱包/电子存折应用(EP,ED)  卡片和终端的接口  卡片本身的技术指标  应用相关的交易流程  终端 ...

  9. nodejs笔记四--创建一个最简单的 express 应用

    express 是 Node.js 应用最广泛的 web 框架,利用 express 可以实现很多的web应用:首先需要需要得到一个express. 新建一个文件夹叫lesson1,进去里面安装 ex ...

  10. 【BZOJ】【1269】【AHOI2006】文本编辑器editor

    Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...