hdu 3449
有依赖的背包,转化成01背包来做;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int f[100009]; int dp[100009]; int main() { int num,value,pen,pp; while(scanf("%d%d",&n,&m)!=EOF) { memset(f,0,sizeof f); for(int i=1; i<=n; i++) { scanf("%d%d",&pp,&num); memcpy(dp,f,sizeof f);//为了减小时间复杂度; while(num--) { scanf("%d%d",&pen,&value); for(int j=m-pp; j>=pen; j--) { dp[j]=max(dp[j],dp[j-pen]+value); } } for(int j=pp; j<=m; j++) { f[j]=max(f[j],dp[j-pp]); } } printf("%d\n",f[m]); } return 0; }