hdu 1561 The more, The Better (树上背包)
The more, The Better
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5516 Accepted Submission(s): 3280
0 1
0 2
0 3
7 4
2 2
0 1
0 4
2 1
7 1
7 6
2 2
0 0
13
思路: 不同于普通的0/1背包,种类之间有约束,需要在有限的范围内进行0/1背包dP
代码:
#include<cstring>
#include<cstdio>
#define NN 205
struct node{
int a,b;
}sac[NN];
bool map[NN][NN];
int dp[NN][NN]; //dp[][]; 表示攻击j城市剩余i次攻击
int n,m;
int max(int a,int b)
{
if(a>b) return a ;
return b;
} void dfs(int st ,int time){
int i;
dp[st][]=sac[st].b;
for( i=;i<=n;i++)
{
if(map[st][i]){
if(<time) dfs(i,time-);
//0/1背包
for(int j=time ;j >= ;j--) {
int v=j+;
for(int k=; k<=j ;k++){
dp[st][v]=max(dp[st][v-k]+dp[i][k],dp[st][v]);
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m)&&m+n){
memset(map,,sizeof map);
memset(dp,,sizeof dp);
for(int i=;i<=n;i++){
scanf("%d%d",&sac[i].a,&sac[i].b);
map[sac[i].a][i]=;
}
dfs(,m+);
printf("%d\n",dp[][m+]);
}
return ;
}
hdu 1561 The more, The Better (树上背包)的更多相关文章
- HDU 1561 The more, The Better(树形背包)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 1011 Starship Troopers(树上背包)
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...
- HDU 1561&HDU 3449 一类简单依赖背包问题
HDU 1561.这道是树形DP了,所谓依赖背包,就是选A前必须选B,这样的问题.1561很明显是这样的题了.把0点当成ROOT就好,然后选子节点前必须先选根,所以初始化数组每一行为该根点的值.由于多 ...
- HDU4044 GeoDefense(有点不一样的树上背包)
题目大概说一棵n个结点的树,每个结点都可以安装某一规格的一个塔,塔有价格和能量两个属性.现在一个敌人从1点出发但不知道他会怎么走,如果他经过一个结点的塔那他就会被塔攻击失去塔能量的HP,如果HP小于等 ...
- HDU 2159 FATE(二维费用背包)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- HDU 1561 树形DP(入门)
题目链接: HDU 1561 The more, The Better #include <iostream> #include <cstdio> #include < ...
- HDU 1712 ACboy needs your help(包背包)
HDU 1712 ACboy needs your help(包背包) pid=1712">http://acm.hdu.edu.cn/showproblem.php? pid=171 ...
- luogu 2014 选课 树上背包
树上背包 #include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; vector<int> ...
- BZOJ 4753 [Jsoi2016]最佳团体 | 树上背包 分数规划
BZOJ 4753 [Jsoi2016]最佳团体 | 树上背包 分数规划 又是一道卡精度卡得我头皮发麻的题-- 题面(--蜜汁改编版) YL大哥是24OI的大哥,有一天,他想要从\(N\)个候选人中选 ...
- 洛谷 P2015 二叉苹果树 (树上背包)
洛谷 P2015 二叉苹果树 (树上背包) 一道树形DP,本来因为是二叉,其实不需要用树上背包来干(其实即使是多叉也可以多叉转二叉),但是最近都刷树上背包的题,所以用了树上背包. 首先,定义状态\(d ...
随机推荐
- CodeForces 483B Friends and Presents
Friends and Presents Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- CodeForces 131A cAPS lOCK
cAPS lOCK Time Limit:500MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit St ...
- Using Post_Query Trigger in Oracle Forms
When a query is open in the block, the Post-Query trigger fires each time Form Builder fetches a rec ...
- spring+hibernate 实体类注解问题
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.Ann ...
- 如何设置table中<tr>和<td>的高度
//-----------------自定义表格table的行和列的宽和高----------------------// 先设置一个样式 如下: <style type="text/ ...
- SAP供应商和客户的创建
进来遇到一个创建供应商的需求,由于在系统中关于供应商和客户的创建比较特殊,且没有相关函数进行创建, 找到一个类和方法来创建,类名:VMD_EI_API 方法名:MAINTAIN_DIRECT_INP ...
- 9月java货车版速记
运算符的优先级java自带的方法正则表达式数组和二维数组:数组遍历,填充数组,数组排序,复制数组,数组查询数组算法:冒泡,选择,反转,快速类和对象:封装,继承,多态,this关键字,抽象类和接口重写和 ...
- VNC的安装与配置
一,安装tigervnc-server VNC软件包 [root@localhost ~]# yum install tigervnc-server 设置开机自启动 [root@localhost ~ ...
- 基础4 Android基础
基础4 Android基础 1. Activity与Fragment的生命周期. Activity生命周期 打开应用 onCreate()->onStart()->onResume 按BA ...
- hibernate Criteria查询多对多(Set集合)条件筛选
PO public class UserPO implements java.io.Serializable { /** 用户唯一标识ID */ private String id; /** 状态(在 ...