【树形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# ...
随机推荐
- webpack 4 :从0配置到项目搭建
webpack4发布以来,我写项目都是用脚手架,即使再简单的项目,真的是really shame..虽然道听途说了很多 webpack4 的特性,却没有尝试过,因为它给人的感觉就是,em...很难.但 ...
- 详解JS中Number()、parseInt()和parseFloat()的区别
三者的作用: Number(): 可以用于任何数据类型转换成数值: parseInt().parseFloat(): 专门用于把字符串转换成数值: 一.Number( ): (1)如果是Boolean ...
- deepin 快捷键
从此脱离鼠标
- Linux-进程间通信(N): 各种IPC的使用场景
1. 管道:只能用于具有亲缘关系的进行通信,使用面相对较窄,实际开发中较少使用: 2. FIFO(命名管道):可以用于任意进程间的通信,对于大块数据的传输效率较高,可应用于单进程大量数据传递,和多个进 ...
- 中断中处理延时及一些函数的调用规则(中断调i2c驱动有感)--中断中的延迟delay与printk函数的冲突【转】
转自:http://blog.csdn.net/psvoldemort/article/details/8222371 1,中断处理程序中不能使用有睡眠功能的函数,如ioremap,kmalloc,m ...
- Android IPC
1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...
- MySQL的sql_mode解析与设置
https://blog.csdn.net/hhq163/article/details/54140286 https://blog.csdn.net/ccccalculator/article/de ...
- aspxgridview export导出数据,把true显示成‘是’
项目原因,数据库中的数据是‘true’还有‘false’,但是在页面上要显示为‘是否’,导出来的时候也是要显示成‘是否’ 要在web页面当中显示成‘是否’,只要在gridview的CustomColu ...
- 自动关闭IO流-jdk1.7版本
public static void main(String[] args) throws IOException { try( FileInputStream fis = new FileInput ...
- LightOJ 1282
Leading and Trailing Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...