【树形dp】Find Metal Mineral
[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
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.
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.
cost.
3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
In the first case: 1->2->1->3 the cost is 3;
In the second case: 1->2; 1->3 the cost is 2;
#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的更多相关文章
- 树形DP-----HDU4003 Find Metal Mineral
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- HDU4003Find Metal Mineral[树形DP 分组背包]
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- hdu 4003 Find Metal Mineral 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...
- HDU4003 Find Metal Mineral 树形DP
Find Metal Mineral Problem Description Humans have discovered a kind of new metal mineral on Mars wh ...
- hdu 4003 Find Metal Mineral 树形dp ,*****
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- HDU 4003 Find Metal Mineral(分组背包+树形DP)
题目链接 很棒的一个树形DP.学的太渣了. #include <cstdio> #include <string> #include <cstring> #incl ...
- HDU-4003 Find Metal Mineral 树形DP (好题)
题意:给出n个点的一棵树,有k个机器人,机器人从根节点rt出发,问访问完整棵树(每个点至少访问一次)的最小代价(即所有机器人路程总和),机器人可以在任何点停下. 解法:这道题还是比较明显的能看出来是树 ...
- 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock's blog】
树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...
- 【DP_树形DP专题】题单总结
转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...
随机推荐
- 正则表达式实现将html文本转换为纯文本格式(将html字符串转换为纯文本方法)
Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase); string strOutput = regex. ...
- centos6.4 yum安装nginx+mysql+php
1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport ...
- perl 在win下输出中文乱码问题
use utf8; my $name = '你好'; binmode(STDOUT, ":encoding(gbk)"); print $name,"\n"; ...
- Python-字符串处理 str.format()
Python中内置的%操作符可用于格式化字符串操作,控制字符串的呈现格式.Python中还有其他的格式化字符串的方式,但%操作符的使用是最方便的. 另外python还有一个更强大的字符串处理函数 st ...
- CentOS在ssh下远程重装系统
CentOS在ssh下远程重装系统 http://www.zxsdw.com/index.php/archives/913/ 国外VPS服务器一般都有控制面板,有很多种系统可自行安装,但国内有些IDC ...
- git subtree:无缝管理通用子项目
移动互联网的爆发以及响应式页面的尴尬症,开发web和mobile项目成为了标配,当然实际情况下,会有更多的项目. 多项目开发对于前端来说是个很大的挑战✦ 重复,重复的前端架构,重复的前端依赖,重复的工 ...
- DateTimeToUnix/UnixToDateTime 对接时间转换
问题,通过毫秒数来解析出时间:(很多对接的时候经常需要用到) <?php $MyJson = '{"jingdong_vas_subscribe_get_responce": ...
- 深度学习方法:受限玻尔兹曼机RBM(一)基本概念
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 最近在复习经典机器学习算法的同 ...
- Balanced Binary Tree——经典题
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- GT-----如何做Android应用流量测试?
1.如何判断一个应用的流量偏高? 如果看流量的绝对值看不出高低,那就找几个同类型的产品对比一下,如果完成同样的事物,被测应用比同类产品高很多,那就偏高了,可能有优化的空间. 2.如何找到有效的优化点? ...