HDU 1561&HDU 3449 一类简单依赖背包问题

HDU 1561。这道是树形DP了,所谓依赖背包,就是选A前必须选B,这样的问题。1561很明显是这样的题了。把0点当成ROOT就好,然后选子节点前必须先选根,所以初始化数组每一行为该根点的值。由于多选了0点,所以记得把m++.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN=222;

int dp[MAXN][MAXN];
struct Edge{
	int u,v,next;
}edge[MAXN];
int head[MAXN],n,m,tot;

void addedge(int u,int v){
	edge[tot].u=u;
	edge[tot].v=v;
	edge[tot].next=head[u];
	head[u]=tot++;
}

void dfs(int u){
	for(int e=head[u];e!=-1;e=edge[e].next){
		int v=edge[e].v;
		dfs(v);
		for(int i=m;i>=1;i--){
			for(int k=1;k<i;k++){
				dp[u][i]=max(dp[u][i],dp[u][i-k]+dp[v][k]);
			}
		}
	}
}

int main(){
	int u,v;
	while(scanf("%d%d",&n,&m),n||m){
		m++;
		memset(dp,0,sizeof(dp));
		memset(head,-1,sizeof(head));
		tot=0;
		for(int i=1;i<=n;i++){
			scanf("%d%d",&u,&v);
			addedge(u,i);
			for(int k=1;k<=m;k++)
			dp[i][k]=v;
		}
		dfs(0);
		printf("%d\n",dp[0][m]);
	}
	return 0;
}

  

HDU 3449.。也是这样的题目,选物品前先买盒子。那么设dp[i][j]为前i个盒子钱为j下最优值。把每个盒子情况当成0/1背包来看。由于先买了盒子,所以初始化时是i-1情况下j-p的最优值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int dp[52][100050];

int main(){
	int n,w,p,k,wi,vi;
	while(scanf("%d%d",&n,&w)!=EOF){
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++){
			scanf("%d%d",&p,&k);
			for(int j=p;j<=w;j++){
				dp[i][j]=dp[i-1][j-p];
			}
			for(int kk=1;kk<=k;kk++){
				scanf("%d%d",&wi,&vi);
				for(int j=w;j>=wi+p;j--){
					dp[i][j]=max(dp[i][j],dp[i][j-wi]+vi);
				}
			}
			for(int j=0;j<=w;j++)
			dp[i][j]=max(dp[i][j],dp[i-1][j]);
		}
		printf("%d\n",dp[n][w]);
	}
	return 0;
}

  

时间: 2024-10-21 11:49:13

HDU 1561&HDU 3449 一类简单依赖背包问题的相关文章

HDU 1561 ——The more, The Better(有依赖的树形DP)

题目分析: 攻占城堡所能获得的最多宝物,但是有个限制,要想攻占某个城堡前必须先攻占另一个城堡 思路: 建图,新建一个根节点为0,那么题目就变为要想取得子节点必须先取得它的父亲节点 今天为了解决这个问题,看了下背包九讲中的有依赖的背包,刷了道模板题. 大概思路是:要想取得附件必须先取主件,主件要么取,要么不取,取得话要怎么分配给附件,用01背包处理附件,然后再把每个主件分组背包 树形的依赖主要是附件是个集合,附件还有附件.森林的概念 根据DP的思想,我们先dfs到叶,01背包操作 即可.只是每次d

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): 4960    Accepted Submission(s): 2927 Problem Description ACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝

hdu 4763 Theme Section (简单KMP)

Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1184    Accepted Submission(s): 621 Problem Description It's time for music! A lot of popular musicians are invited to join us in t

HDU 1846 Brave Game (简单博弈)

HDU 1846 Brave Game (简单博弈) ACM 题目地址: HDU 1846 Brave Game 题意: 中文. 分析: 博弈入门. 如果n=m+1,因为最多取m个,所以先拿的人拿多少个,后拿的人能全拿走. 所以判断n%(m+1)即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1846.cpp * Create Date: 2014-09-20 10:05:26 * Descripton: game */ #

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU 4960 Another OCD Patient 简单DP

思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algori

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

hdu 2999 sg函数(简单博弈)

Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 393    Accepted Submission(s): 132 Problem Description "Alice and Bob are playing stone game...""E

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the