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 ...
随机推荐
- [转]What you need to know about transimpedance amplifiers – part 1
Transimpedance amplifiers (TIAs) act as front-end amplifiers for optical sensors such as photodiodes ...
- wince下的CPU和内存占用率计算
#include <Windows.h> DWORD Caculation_CPU(LPVOID lpVoid) { MEMORYSTATUS MemoryInfo; DWORD Perc ...
- hdu 4163 Stock Prices 水
#include<bits/stdc++.h> using namespace std; #define ll long long #define pi (4*atan(1.0)) #de ...
- HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- iOS - Widget 小部件
1.Widget iOS extension 的出现,方便了用户查看应用的服务,比如用户可以在 Today 的 widgets 中查看应用的简略信息,然后点击进入相关的应用界面. 2.添加 Widge ...
- iOS - UIWebView
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...
- URLConnection 使用
统一资源定位符URL(Uniform Resource Locator)是www客户机访问Internet时用来标识资源的名字和地址.超文本链路由统一资源定位符URL维持.URL的格式是: <M ...
- poj2451Uyuw's Concert(半平面交)
链接 逆时针给出线段,如果模板是顺时针的修改下系数的符号进行平面交即可. #include <iostream> #include<cstdio> #include<cs ...
- linux之flock函数锁文件
头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依参数operation所指定 ...
- python中的最最最基本语法(1)
注意:对于我这个以前用c/c++的同学来说,可能一开始学习pyhon时有点不适应的,为什么呢?因为吧,python中,没有这玩意:{},也不用每句话才用分号分开的.python中通过缩进来分块的,一行 ...