博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU4003Find Metal Mineral[树形DP 分组背包]
阅读量:6277 次
发布时间:2019-06-22

本文共 2881 字,大约阅读时间需要 9 分钟。

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
#include
#include
#include
#include
using namespace std;const int N=1e4+5,M=12;int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){
if(c=='-')f=-1; c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();} return x*f;}struct edge{ int v,w,ne;}e[N<<1];int h[N],cnt=0;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>=0;j--){ //ti ji f[u][j]+=f[v][0]+w*2;//must choose for(int k=1;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=0; memset(h,0,sizeof(h)); for(int i=1;i<=n-1;i++){ u=read();v=read();w=read(); ins(u,v,w); } memset(f,0,sizeof(f)); dp(s,0); printf("%d\n",f[s][m]); } return 0;}

 

转载地址:http://eufva.baihongyu.com/

你可能感兴趣的文章
Qt下QTableWidget的使用
查看>>
mybatis 针对SQL Server 的 主键id生成策略
查看>>
vmware安装centOs操作系统配置网络的一系列问题
查看>>
jquery基本选择器
查看>>
2天时间终于把ntopng装好了
查看>>
Mac配置环境变量注意点
查看>>
Lua string.gsub (s, pattern, repl [, n])
查看>>
智能聊天机器人实现(源代码+解析)
查看>>
微表面分布函数(Microfacet Distribution Function)确切含义
查看>>
轻松python文本专题-字符与字符值转换
查看>>
JAVA-MyEclipse第一个实例
查看>>
iOS 9 学习系列: Xcode Code Coverage
查看>>
休眠模式的开关闭
查看>>
Variable number of arguments (Varargs)
查看>>
jquery.ajax之beforeSend方法使用介绍
查看>>
usb键鼠驱动分析【钻】
查看>>
shell中while循环的陷阱
查看>>
Java 系书籍,,,,,,,,,,,,,
查看>>
binlog 轻松的找到没有及时提交的事物(infobin工具
查看>>
windows下如何创建没有名字的.htaccess文件
查看>>