标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19849 Accepted Submission(s): 10086
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; const int MAXN=10005; int n,W; int v[MAXN],w[MAXN]; int dp[MAXN]; int main() { int T; scanf("%d",&T); while(T--) { int e,f; scanf("%d%d",&e,&f); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d%d",&v[i],&w[i]); } W=f-e; for(int i=0;i<MAXN;i++) { dp[i]=INF;//大前提:背包装满.因为求获得的价值最小,所以需要将dp初始化正无穷.若求价值最大,则将dp初始化为负无穷 } dp[0]=0;//背包需要装满,则将dp[0]初始化为0 for(int i=0;i<n;i++) { for(int j=w[i];j<=W;j++) //完全背包:逆序 dp[j]=min(dp[j-w[i]]+v[i],dp[j]);//求最小的价值 } if(dp[W]==INF) { printf("This is impossible.\n"); } else { printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5633151.html