完全背包

来源:互联网 发布:python进入命令行模式 编辑:程序博客网 时间:2024/06/05 07:27
完全背包是指 物品无限使用的,所以在枚举的时候要将for循环用++的方式来枚举。#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespace std;const int maxn=10010;int s[maxn],t[maxn],dp[maxn];int main(){memset(s,0,sizeof(s));memset(t,0,sizeof(t));memset(dp,0,sizeof(dp));int m,n,i,j,k;scanf("%d%d",&n,&m);for(i=1;i<=m;i++)scanf("%d%d",&s[i],&t[i]);for(i=1;i<=m;i++)for(int j=t[i];j<=n;j++)//与01背包的差别就在这里 dp[j]=max(dp[j],dp[j-t[i]]+s[i]);printf("%d",dp[n]);return 0;}

1 0
原创粉丝点击