[HDU4003]Find Metal Mineral

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

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;

 
Source
 
题目大意:给出一颗带边权树,N个节点,根为S。现在有K个机器人要遍历这N个点,求最小花费?
试题分析:依照以前的设计思路,还是dp[N][K]表示在节点i的子树中用去j个机器人状态。
     那么这样设计可不可行呢?我们来看一下转移方程:
       那么就需要将j=0单列出来讨论 dp[i][0]=sum(dp[i->son][0]+2*Cost[i->son]) 因为只有一个机器人,所以只能它自己走,结果一定。
             j≠0时:     dp[i][j]=min(dp[i][j],dp[i->son][t]+t*Cost[i->son]+dp[i][j-t]);有多个机器人时,肯定让它们分开跑最优。
     dp[i][j]每次应该先加上dp[i->son][0]+2*Cost[i->son],因为可能用一个机器人遍历这颗子树。
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,S,K;
int u,w,v;
int Next[MAXN*2],Node[MAXN*2],Root[MAXN*2],Cost[MAXN*2];
int dp[MAXN][12];
int cnt; void addedge(int u,int v,int w){
cnt++;
Node[cnt]=v;
Cost[cnt]=w;
Next[cnt]=Root[u];
Root[u]=cnt;
return ;
} void dfs(int x,int fa){
int cnt=0;
for(int x1=Root[x];x1;x1=Next[x1]){
if(Node[x1]==fa) continue;
dfs(Node[x1],x);cnt++;
}
if(!cnt) {
for(int i=0;i<=K;i++) dp[x][i]=0;
return ;
}
for(int x1=Root[x];x1;x1=Next[x1]){
if(Node[x1]==fa) continue;
int SON=Node[x1];
for(int k=K;k>=0;k--){
if(dp[x][k]==-1){
if(!k) dp[x][0]=dp[SON][0]+Cost[x1]*2;
else{
for(int t=1;t<=k;t++)
if(dp[x][k]==-1||dp[x][k]>dp[SON][t]+t*Cost[x1])
dp[x][k]=dp[SON][t]+t*Cost[x1];
}
}
else{
if(!k) dp[x][k]+=(dp[SON][0]+Cost[x1]*2);
else{
dp[x][k]+=dp[SON][0]+2*Cost[x1];
for(int t=1;t<=k;t++){
dp[x][k]=min(dp[x][k-t]+dp[SON][t]+t*Cost[x1],dp[x][k]);
}
}
}
}
}
return ;
} int main(){
while(scanf("%d%d%d",&N,&S,&K)!=EOF){
cnt=0;
memset(Node,0,sizeof(Node));
memset(Root,0,sizeof(Root));
memset(Cost,0,sizeof(Cost));
memset(Next,0,sizeof(Next));
for(int i=1;i<N;i++){
u=read(),v=read(),w=read();
addedge(u,v,w);
addedge(v,u,w);
}
memset(dp,-1,sizeof(dp));
dfs(S,-1);
printf("%d\n",dp[S][K]);
}
}

【树形dp】Find Metal Mineral的更多相关文章

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

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

  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. HDU4003 Find Metal Mineral 树形DP

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

  5. hdu 4003 Find 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)

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

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

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

  8. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】

    树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

  9. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

随机推荐

  1. 苹果API常用英语名词---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 苹果API常用英语名词0. indicating 决定1.in order to 以便 ...

  2. ES6数组去重及ES5数组去重方法

    ES6中新增了Set数据结构,类似于数组,但是 它的成员都是唯一的 ,其构造函数可以接受一个数组作为参数,如: let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3]; ...

  3. 八大疯狂的HTML5 Canvas及WebGL动画效果——8 CRAZY ANIMATIONS WITH WEBGL AND HTML5 CANVAS【收藏】

    HTML5, WebGL and Javascript have changed the way animation used to be. Past few years, we can only a ...

  4. Python 关于拷贝(copy)汇总(列表拷贝 // 字典拷贝 // 自定义对象拷贝)

    1.列表拷贝 引用是指保存的值为对象的地址.在 Python 语言中,一个变量保存的值除了基本类型保存的是值外,其它都是引用,因此对于它们的使用就需要小心一些.下面举个例子: 问题描述:已知一个列表, ...

  5. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  6. spring mvc 提供的几个常用的扩展点

    转载 :http://blog.csdn.net/gufachongyang02/article/details/43836105 这是spring3 mvc的核心流程图:   SpirngMVC的第 ...

  7. Spring的BeanFactory体系结构(一)

    本文使用的代码是: Spring 3.0 接 触Spring也有很长一段时间了.但是,每次都是直接使用Spring直接提供的API,时间久了,自然也会想探索Spring里面的奥秘.今天上 午,整理出了 ...

  8. face_recognition 人脸识别报错

    [root@localhost examples]# python facerec_from_video_file.py RuntimeError: module compiled against A ...

  9. 自己动手实现arm函数栈帧回溯【转】

    转自:http://blog.csdn.net/dragon101788/article/details/18668505 内核版本:2.6.14 glibc版本:2.3.6 CPU平台:arm gl ...

  10. device tree property ---- interrupt-names

    device tree source 的 interrupt-names property 會對應到 pltform_get_irq_byname() 的第二個參數. .dtsi or .dts in ...