hdu4003详解(树形dp+多组背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003
Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4490 Accepted Submission(s): 2071
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.
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
2
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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct{//链式前向星
int v,w,next;
}edge[];
int head[];
int cnt;
bool vt[];
int dp[][];//dp[i][j]表示节点 i 损失(机器人最后不回到i节点) j 个机器人遍历以节点 i 为根节点组成的子树的所有点所需的最小能量
void add(int u,int v,int w){
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int n,s,m;
void dfs(int k){
for(int i=;i<=m;i++)//初始化为0
dp[k][i]=;
vt[k]=true;
for(int i=head[k];i!=-;i=edge[i].next){
if(!vt[edge[i].v]){
dfs(edge[i].v);//先递归子节点
for(int j=m;j>=;j--){
dp[k][j]+=dp[edge[i].v][]+*edge[i].w;//它儿子的花费的机器人是0,我就要派一个机器人去它那里然后再叫它回来
for(int l=;l<=j;l++){
dp[k][j]=min(dp[k][j],dp[k][j-l]+dp[edge[i].v][l]+edge[i].w*l);//它儿子要花费l个机器人,那我要派l个过去
}
} }
}
}
int main(){
int u,v,w;
while(scanf("%d%d%d",&n,&s,&m)!=EOF){
fill(head,head+,-);//初始化
fill(vt,vt+,false);
cnt=;
for(int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dfs(s);
printf("%d\n",dp[s][m]);
}
return ;
}
hdu4003详解(树形dp+多组背包)的更多相关文章
- CH5402 选课【树形DP】【背包】
5402 选课 0x50「动态规划」例题 描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了 N(N≤300) 门的选修课程,每个学生可选课程的数量 M 是 ...
- 树形DP+(分组背包||二叉树,一般树,森林之间的转换)codevs 1378 选课
codevs 1378 选课 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 学校实行学分制.每门的必修课都有固定的学分 ...
- hdu1561 树形dp,依赖背包
多重背包是某个物品可以选择多次,要把对物品数的枚举放在对w枚举外面 分组背包是某组的物品只能选一个,要把对每组物品的枚举放在对w枚举内侧 依赖背包是多层的分组背包,利用树形结构建立依赖关系,每个结点都 ...
- Codevs1378选课[树形DP|两种做法(多叉转二叉|树形DP+分组背包)---(▼皿▼#)----^___^]
题目描述 Description 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修 ...
- UVA Live Archive 4015 Cave (树形dp,分组背包)
和Heroes Of Might And Magic 相似,题目的询问是dp的一个副产物. 距离是不好表示成状态的,但是可以换一个角度想,如果知道了从一个点向子树走k个结点的最短距离, 那么就可以回答 ...
- HD1561The more, The Better(树形DP+有依赖背包)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)
The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...
- poj3345 Bribing FIPA【树形DP】【背包】
Bribing FIPA Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5910 Accepted: 1850 Desc ...
- Android长度单位详解(dp、sp、px、in、pt、mm、dip)
Android中定义的dimension单位有以下这些:px(Pixels ,像素):对应屏幕上的实际像素点.in(Inches ,英寸):屏幕物理长度单位.mm(Millimeters ,毫米):屏 ...
随机推荐
- js 延时等待
//延时器,2秒后执行函数 function test(){ alert("aaaa"); } setTimeout(function () { test(); }, ); //或 ...
- HDU 1024 Max Sum Plus Plus(DP的简单优化)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- HTML标记
html:超文本标记语言 标记,标签,元素 单标签:<标签名 属性名="属性值" 属性名=“属性值” /> 双标签:<标签名 属性名="属性值” 属 ...
- SSM框架完成Ajax简单用户登录验证
一.前端JSP <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...
- ionic3 在windows环境下打包android 正式签名版APK
生成签名文件keytool -genkey -alias demo.keystore -keyalg RSA -validity 40000 -keystore demo.keystore demo为 ...
- linux软件管理之python包管理
Python包管理 ====================================================================================python ...
- 初识Log4Net
刚刚了解log4net,根据自己的理解翻译了一下比较重要的东西.详细状况请见log4net官方网站 log4net是一种帮助程序员将日志语句输出到各种目标文件的输出工具,有了log4net,就可以在运 ...
- php代码审计——DVWA
1.命令注入: LOW等级: 输入没有任何限制,可以直接ping: Medium等级: 介绍一下str_replace()函数和array_key()函数: Str_replace(str1,str2 ...
- licode测试
https://github.com/lynckia/licode/tree/master/test 使用js模拟客户端调用,也可以使用mocha来进行同样的测试
- OO的奇妙冒险1
OO的奇妙冒险 ~OOP入门与字符串处理~ 目录 总体分析 作业内容分析 作业内容总结 互测的收获 公测互测bug分析与总结 不太正经的个人自嗨 总体分析 公测 中测(基础与进阶): 其实在我看来,从 ...