题意:

一棵有权树,从根结点中放入 K 个机器人。求用这 K 个机器人遍历全部的结点最少的权值和。

思路:

1. dp[u][i] 表示给以 u 为根节点的子树放 i 个机器人,遍历其子树所须要的最小权值。

2. 关键在于 dp[u][0] 的理解,表示:最后停留在以 u 为根节点的子树下 0 个机器人,而且遍历了 u 子树的最小权值和。

3. 以下的步骤就变成和分组背包类似的情况了,根节点 u 给孩子 v 放多少个机器人。

4. dp[u][i] = min(dp[u][i], dp[u][j] + dp[v][i-j] + (i-j) * c); 能够理解成给 v 放了 i-j 个机器人,给 v 的其它兄弟放了 j 个,u 总共同拥有 i 个

Find Metal Mineral

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 2354    Accepted Submission(s): 1071

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
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=11000; struct Edge
{
int to,next,w;
}edge[2*maxn]; int Adj[maxn],Size; void init()
{
memset(Adj,-1,sizeof(Adj)); Size=0;
} void Add_Edge(int u,int v,int weight)
{
edge[Size].to=v;
edge[Size].next=Adj[u];
edge[Size].w=weight;
Adj[u]=Size++;
} int n,S,K;
int dp[maxn][20];
bool vis[maxn]; void dfs(int u)
{
vis[u]=true;
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int w=edge[i].w;
if(vis[v]) continue; dfs(v); for(int j=K;j>=0;j--)
{
dp[u][j]+=dp[v][0]+2*w;
for(int jj=0;jj<=j;jj++)
{
dp[u][j]=min(dp[u][j],dp[u][j-jj]+dp[v][jj]+jj*w);
}
}
} } int main()
{
while(scanf("%d%d%d",&n,&S,&K)!=EOF)
{
init();
for(int i=1;i<n;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
Add_Edge(a,b,w);
Add_Edge(b,a,w);
}
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
dfs(S);
printf("%d\n",dp[S][K]);
}
return 0;
}

HDOJ 4003 Find Metal Mineral的更多相关文章

  1. hdu 4003 Find Metal Mineral 树形DP

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

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

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

  4. HDU 4003 Find Metal Mineral (树形DP,经典)

    题意:给定一棵树图,n个节点,有边权,要派k<11个机器人从节点s出发,遍历所有的点,每当1只机器人经过1条边时就会花费该边的边权,边是可重复走的.问遍历完所有点的最小花费? 思路: 非常经典, ...

  5. HDU 4003 Find Metal Mineral

    这个题是POJ1849的加强版. 先说一个很重要的结论,下面两种方法都是从这个结论出发的. 一个人从起点遍历一颗树,如果最终要回到起点,走过的最小权值就是整棵树的权值的2倍. 而且K个人的情况也是如此 ...

  6. xtu summer individual 6 E - Find Metal Mineral

    Find Metal Mineral Time Limit: 1000ms Memory Limit: 65768KB This problem will be judged on HDU. Orig ...

  7. HDU4003Find Metal Mineral[树形DP 分组背包]

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

  8. HDU4003 Find Metal Mineral 树形DP

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

  9. HDU4003 Find Metal Mineral

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

随机推荐

  1. $monitor用法

    1.$monitor 进程同一时间有且仅有一个,若多次调用$monitor,新进程会代替以前的monitor进程. 2.$fmonitor可以同时存在任意个. 3.一般不用$monitor系统函数. ...

  2. Knockout v3.4.0 中文版教程-7-计算监控-依赖跟踪如何工作

    3.依赖跟踪如何工作 初学者不需要知道这一点,但更高级的开发人员将想知道为我们怎么实现KO自动跟踪依赖性和自动更新UI的正确部分... 它其实相当简单优雅,跟踪算法如下: 当你定义一个计算监控,KO立 ...

  3. PYDay2-linux基础\常用命令

    一.linux 理念 一切皆文件 二.常用命令(150) 2.1.rsync rsync是类unix系统下的数据镜像备份工具, 它的特性如下: 可以镜像保存整个目录树和文件系统. 可以很容易做到保持原 ...

  4. svg path 动画效果

    http://www.zhangxinxu.com/wordpress/2014/04/animateion-line-drawing-svg-path-%E5%8A%A8%E7%94%BB-%E8% ...

  5. Python第三方库之openpyxl(10)

    Python第三方库之openpyxl(10) 雷达图 在工作表上的列或行中排列的数据可以在雷达图中绘制.雷达图比较多个数据系列的总值.它实际上是一个圆形x轴上的面积图的投影.有两种类型的雷达图:st ...

  6. NYOJ 814 又见拦截导弹

    又见拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 大家对拦截导弹那个题目应该比较熟悉了,我再叙述一下题意:某国为了防御敌国的导弹袭击,新研制出来一种导弹拦 ...

  7. Leetcode 330.按要求补齐数组

    按要求补齐数组 给定一个已排序的正整数数组 nums,和一个正整数 n .从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数 ...

  8. 【Python】SyntaxError: Non-ASCII character '\xe8' in file

    遇到的第一个问题: SyntaxError: Non-ASCII character '\xe8' in file D:/PyCharmProject/TempConvert.py on line 2 ...

  9. Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)

    G. Fake News (easy) time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. 九度oj 题目1537:买卖股票

    题目描述: 给定一个大小为n的数组,数组的元素a[i]代表第i天的股票价格. 设计一个算法,计算在最多允许买卖k次(一买一卖记为一次)的条件下的最大收益. 需要注意的是,你不能同时拥有两份股票.也就是 ...