SoL:完全背包。。。
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int maxm = 10000 + 10; const int maxn = 50 + 10; int dp[maxm]; int value[maxn];//每袋的价格 int weight[maxn];//每袋的重量 int nValue,nKind; //完全背包,代价为cost,获得的价值为weight void CompletePack(int cost,int weight) { for(int i=cost;i<=nValue;i++) dp[i]=min(dp[i],dp[i-cost]+weight); } int main() { while(~scanf("%d%d",&nValue,&nKind)) { for(int i=0;i<nKind;i++) scanf("%d%d",&weight[i],&value[i]); for(int i=0;i<=nValue;i++) dp[i]=INF; dp[0]=0; for(int i=0;i<nKind;i++) CompletePack(value[i],weight[i]); if(dp[nValue]>=INF) printf("-1\n"); else printf("%d\n",dp[nValue]); } return 0; }