[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. DotNet 学习笔记 Servers

    Servers ASP.NET Core ships with two different HTTP servers: •Microsoft.AspNetCore.Server.Kestrel (AK ...

  2. ThinkSnS v4后台任意文件下载漏洞

    漏洞文件: /apps/admin/Lib/Action/UpgradeAction.class.php 主要问题还是出现在了180行直接将远程获取到的图片直接保存. 文中可见并没有做任何的对$dow ...

  3. POJ1014(多重背包)

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65044   Accepted: 16884 Descri ...

  4. linux下实现在程序运行时的函数替换(热补丁)【转】

    转自:http://www.cnblogs.com/leo0000/p/5632642.html 声明:以下的代码成果,是参考了网上的injso技术,在本文的最后会给出地址,同时非常感谢injso技术 ...

  5. linux===启动sdk manager下载配置sdk的时候报错的解决办法

    当启动sdk manager下载配置sdk的时候,报错如下: botoo@botoo-virtual-machine:/opt/android-sdk-linux/tools$ sudo  ./and ...

  6. 【bzoj4373】算术天才⑨与等差数列

    同之前那道由乃题,可以认为由乃题是这题的特殊情况…… 维护方法是同样的,维护区间和,区间平方和即可. 注意特判一个数(其实没有必要) #include<bits/stdc++.h> ; u ...

  7. 搭建selenium+python自动化环境

    1.安装python,下载地址:http://python.org---安装版本3.5.1 ps:自带setuptools和pip工具 2.然后,用pip安装开发Web App需要的第三方库:异步框架 ...

  8. ES6 promise简单实现

    基本功能实现: function Promise(fn){ //需要一个成功时的回调 var doneCallback; //一个实例的方法,用来注册异步事件 this.then = function ...

  9. MYSQL 索引无效和索引有效的详细介绍

    1.WHERE字句的查询条件里有不等于号(WHERE column!=...),MYSQL将无法使用索引 2.类似地,如果WHERE字句的查询条件里使用了函数(如:WHERE DAY(column)= ...

  10. leetcode 之Reverse Linked List II(15)

    这题用需要非常细心,用头插法移动需要考虑先移动哪个,只需三个指针即可. ListNode *reverseList(ListNode *head, int m, int n) { ListNode d ...