Find Metal Mineral

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

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

终于填坑了
和选课一样,树形DP,每个子节点是一个分组,不同的组是放的机器人不同
f[i][j]表示子树i放j个机器人(不用上来)的最小代价
f[i][0]表示放一个又上来   可以发现放多个有上来的不可能更优
因为每组至少选一个,所以先把f[v][0]放上
//
// main.cpp
// hdu4003
//
// Created by Candy on 9/23/16.
// Copyright © 2016 Candy. All rights reserved.
// #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1e4+,M=;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
struct edge{
int v,w,ne;
}e[N<<];
int h[N],cnt=;
void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;
}
int n,s,m,u,v,w;
int f[N][M];
void dp(int u,int fa){
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(v==fa) continue;
dp(v,u);
for(int j=m;j>=;j--){ //ti ji
f[u][j]+=f[v][]+w*;//must choose
for(int k=;k<=j;k++)//group
f[u][j]=min(f[u][j],f[u][j-k]+f[v][k]+w*k);
}
}
}
int main(int argc, const char * argv[]) {
while(cin>>n>>s>>m){
cnt=;
memset(h,,sizeof(h));
for(int i=;i<=n-;i++){
u=read();v=read();w=read();
ins(u,v,w);
}
memset(f,,sizeof(f));
dp(s,);
printf("%d\n",f[s][m]);
}
return ;
}

HDU4003Find Metal Mineral[树形DP 分组背包]的更多相关文章

  1. hdu4003Find Metal Mineral(树形DP)

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

  2. hdu 4003 Find Metal Mineral 树形DP

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

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

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

  4. HDU-1011 Starship Troopers (树形DP+分组背包)

    题目大意:给一棵有根带点权树,并且给出容量.求在不超过容量下的最大权值.前提是选完父节点才能选子节点. 题目分析:树上的分组背包. ps:特判m为0时的情况. 代码如下: # include<i ...

  5. HDU4003 Find Metal Mineral 树形DP

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

  6. Ural-1018 Binary Apple Tree(树形dp+分组背包)

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #i ...

  7. hdu 1561 树形dp+分组背包

    题意:就是给定n个点,每个地点有value[i]的宝物,而且有的宝物必须是另一个宝物取了才能取,问取m个点可以获得的最多宝物价值. 一个子节点就可以返回m个状态,每个状态表示容量为j(j<=m) ...

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

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

  9. 【P2015】二叉苹果树 (树形DP分组背包)

    题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 现在这颗树枝条太多了,需要剪枝.但是 ...

随机推荐

  1. Oracle中用随机数更新字段----将一张表的数据插入另一张表----环境设置

    DECLARE CURSOR recordCursor IS SELECT longitude,latitude FROM WR_WIUST_B_SEC FOR UPDATE; recordRow r ...

  2. MSCRM 报表显示 rsprocessingaborted 错误

    今天又有朋友遇到rsprocessingaborted这个问题,想想这个已经是很老很老的问题了,得在写一遍补充下. 一.首先会考虑是不是SrsDataConnector没有安装的原因,如果正常安装的话 ...

  3. 转-Nmap扫描原理与用法

    1     Nmap介绍 操作系统与设备类型等信息. Nmap的优点: 1.      灵活.支持数十种不同的扫描方式,支持多种目标对象的扫描. 2.      强大.Nmap可以用于扫描互联网上大规 ...

  4. Day Tips:分布式缓存的删除和重建

    遇到cacheHostInfo is null 错误时,必须将这台服务器上的实例删除重新创建 $instanceName ="SPDistributedCacheService Name=A ...

  5. javascript中,如何判断input中输入的为纯数字

    用正则表达式判断.如果纯数字是指整数的话(不包含小数点),可以这样: function check(){ var value = document.getElementById("input ...

  6. 使用sencha cmd 一键生成你的应用程序代码

    一键生成你的应用程序代码: ------------------------------------------------------------ 我们的出发点就是使用命令来产生一个应用程序,执行以 ...

  7. hypervisor与VMware共存方法

    bcdedit /set hypervisorlaunchtype offbcdedit /set hypervisorlaunchtype auto

  8. WordCount的程序设计没写出来怎么办

    这一星期要完成三个小作业,完成前两个已经让我很吃力的了,现在这个WordCount的编程我都没有头绪,不知道从何下手.虽然要求很看起来很简单,可是不知道怎么去设计这个程序,这两天我也在积极找书学习相关 ...

  9. Images.xcassets

    Images.xcassets 概述 功能 方便用户管理图像资源. 图片获取方式 Images.xcassets中的图片资源只能通过imageNamed:方法加载,通过NSBundle的pathFor ...

  10. 500 OOPS: vsftpd: refusing to run with writable root inside chroot()

    Ubuntu 12.04 64bit系统下安装的vsftpd,在登陆时提示500 OOPS: vsftpd: refusing to run with writable root inside chr ...