hdu 5410
来源:互联网 发布:手机数据连接不能上网 编辑:程序博客网 时间:2024/04/29 12:05
写法一 滚动数组+dp
dp[c][i][0] 表示花了i元不取当前这种物品,dp[c][i][1] 表示花了i元取过当前这种物品
#include<stdio.h>#include<string>#include<map>#include<vector>#include<cmath>#include<stdlib.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;const int N=1e4+10;const int MOD=2015;const int inf=1e9;int n,m,k;long long dp[2][2*N][2];int a[N],b[N],w[N];int main(){#ifndef ONLINE_JUDGEgers Wi, Ai and Bi. freopen("aaa","r",stdin);#endif int T; scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); memset(dp,0,sizeof dp); for(int i=1;i<=n;i++){ scanf("%d%d%d",&w[i],&a[i],&b[i]); } long long res=0; int c=1; for(int i=0;i<=m;i++) dp[c^1][i][0]=dp[c^1][i][1]=-inf; dp[c^1][0][0]=0; for(int i=1;i<=n;i++){ int u=c^1; for(int j=0;j<=m;j++){ if(j<w[i]){ dp[c][j][0]=max(dp[u][j][1],dp[u][j][0]); dp[c][j][1]=-inf; }else{ dp[c][j][1]=max(dp[c][j-w[i]][0]+a[i]+b[i],dp[c][j-w[i]][1]+a[i]); dp[c][j][0]=max(dp[u][j][1],dp[u][j][0]); } res=max(res,max(dp[c][j][0],dp[c][j][1])); } c^=1; } printf("%I64d\n",res); } return 0;}
写法2 01背包加完全背包
#include<stdio.h>#include<string>#include<map>#include<vector>#include<cmath>#include<stdlib.h>#include<string.h>#include<algorithm>#include<iostream>using namespace std;const int N=1024;const int MOD=2015;const int inf=1e9;int n,m,k;int dp[2048];int a[N],b[N],w[N];int main(){#ifndef ONLINE_JUDGE freopen("aaa","r",stdin);#endif int T; scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); for(int i=0;i<n;i++) scanf("%d%d%d",&w[i],&a[i],&b[i]); memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { for(int j=m;j>=w[i];j--) dp[j]=max(dp[j],dp[j-w[i]]+a[i]+b[i]); for(int j=w[i];j<=m;j++) dp[j]=max(dp[j],dp[j-w[i]]+a[i]); } printf("%d\n",dp[m]); } return 0;}
0 0
- hdu 5410
- hdu 5410
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- 【Redis基础】Redis之Windows上安装&在Java中的使用
- 记录一次mysql从库恢复并开启主从复制的过程
- EL表达式
- 【OpenCV_08】直方图均衡化Histogram Equalization
- html rem动态计算
- hdu 5410
- 图像检索大牛芮勇Yong Rui
- XPath
- 完全背包问题UVA147
- IDM张馨予版
- 安卓IPC机制之Binder详解
- [leetcode]93. Restore IP Addresses
- RRC信令过程概述
- Intent、PendingIntent的区别