杭电oj 1114 完全背包

#include
#include
using namespace std;

#define INF 0x7fffffff

struct mon{
 int w;
 int v;
}list[100];

void main()
{
 int dp[100];
 int n;
 int emp;
 int sum,k;//钱总重,钱种类
 while (cin >> n)
 {
  while (n--)
  {
   scanf("%d%d%d", &emp, &sum,&k);
   sum -= emp;//钱重
   for (int i = 1; i <= k; i++)
    scanf("%d%d", &list[i].v, &list[i].w);
   //memset(dp,INF, sizeof(dp));
   for (int i = 1; i <= sum; i++) //因为要求刚好装满,所以其他的取不到都赋值为无穷
    dp[i] = INF;
   dp[0] = 0;
   for (int i = 1; i <= k; i++)
    for (int j = list[i].w; j <= sum; j++)
    {
     if (dp[j - list[i].w] != INF)
      dp[j] = min(dp[j], dp[j - list[i].w] + list[i].v);
    }
   if (dp[sum] != INF)
    printf("The minimum amount of money in the piggy-bank is %d\n", dp[sum]);
   else printf("This is impossible.");

  }

 

你可能感兴趣的:(杭电OJ,DP)