https://vjudge.net/contest/68966#problem/F
初始化就行了;dp【0】=0;
这题还要刚好装满背包,输出时进行判断
#include<map> #include<set> #include<list> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long using namespace std; const int N=10005,inf=0x3f3f3f; int n,v[N],w[N],dp[N]; int main() { int t; scanf("%d",&t); while(t--){ int a,b,n,W; scanf("%d%d",&a,&b); W=b-a; memset(dp,inf,sizeof(dp)); dp[0]=0; scanf("%d",&n); for(int i=0;i<n;i++)scanf("%d%d",&v[i],&w[i]); for(int i=0;i<n;i++) for(int j=w[i];j<=W;j++) dp[j]=min(dp[j],dp[j-w[i]]+v[i]); if(dp[W]>=inf)printf("This is impossible. "); else printf("The minimum amount of money in the piggy-bank is %d. ",dp[W]); } return 0; }