题目链接

  题意:已知n种coin的价值和体积  求装满容量为v背包的最小硬币价值

 1 #include <algorithm>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 const int INF=0x3f3f3f3f;
 7 int dp[507][10007];//前i个元素将j装满的最小价值
 8 int p[507],w[507];
 9 int n,v;
10 int main ()
11 {
12     int T; scanf ("%d",&T);
13     while (T--) {
14         memset (dp,0x3f,sizeof(dp));
15         dp[0][0]=0;
16         int v1,v2; scanf ("%d %d",&v1,&v2);
17         v=v2-v1; 
18         scanf ("%d",&n);
19         for (int i=1;i<=n;i++)
20             scanf ("%d %d",&p[i],&w[i]);
21         for (int i=1;i<=n;i++) 
22             for (int j=0;j<=v;j++) {
23                 dp[i][j]=dp[i-1][j];
24                 if (j>=w[i])
25                     dp[i][j]=min (dp[i][j],dp[i][j-w[i]]+p[i]);
26             }
27         if (dp[n][v]==INF) printf("This is impossible.\n");
28         else               printf("The minimum amount of money in the piggy-bank is %d.\n",dp[n][v]);
29     }
30     return 0;
31 }