[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. Linux 中使用 dd 测试磁盘性能

    翻译自 : Linux I/O Performance Tests using dd 基本说明 dd 可以用来做简单的低级别复制文件. 这样做, 一般都是可一直直接访问设备文件. 需要说明的是, 错误 ...

  2. SCU 1029 Humble Numbers (打表处理)

    题目链接 题意:素因子中只有2 3 5 7的数称为谦逊的数,1也是谦逊的数,题目中已经给出了前20个谦逊的数.给定数字n,按格式输出第n个谦逊的数. 题解:打表即可,注意打表的技巧就行了. ps:这道 ...

  3. HDU 1728 逃离迷宫 (广搜)

    题目链接 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  4. nginx路由文件配置

    nginx中文文档 Nginx 的请求处理有多个阶段,比如说rewrite.access.content等等,不同的配置字段属于不同的配置阶段,不同阶段的先后执行顺序不一样,例如rewrite在con ...

  5. Java初次见面

    1.Java语言特点(运行环境JRE[操作系统,api,dll]): a.跨平台:Java自带的虚拟机很好地实现了跨平台性.Java源程序代码经过编译后生成二进制的字节码是与平台无关的,但是可被Jav ...

  6. JS判断客户端是否是iOS或者Android手机移动端

    var u = navigator.userAgent; var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > - ...

  7. leetcode 之Implement strStr()(27)

    字符串的匹配,返回匹配开始的位置,直接用暴力方式求解.为了更快的匹配,定义一个指针表示待匹配的字符串的长度,当长度不足时,可 直接停止匹配. char *strStr(char *haystack, ...

  8. awk处理之案例六:awk根据条件插入文本

    编译环境 本系列文章所提供的算法均在以下环境下编译通过. [脚本编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...

  9. es6中对象的一些操坐

    变量的赋值 key值得构建 对象的严格检测 对象的合并 1.变量的赋值: let name='宋宇',age='17岁': let obj={name,age} //快速的将变量引入到对象中去. 2. ...

  10. CentOS7英文环境下使用中文输入法

    一般我们使用英文环境是没有办法使用中文的,这是因为我们没有设置在英文环境下的中文配置,下面我们在英文环境下配置中文输入法ibus使得我们可以输入中文. 首先,安装ibus(centos6以后已经默认安 ...