poj1742andHDOJ2844(DP多重背包)

来源:互联网 发布:手机怎样看淘宝的积分 编辑:程序博客网 时间:2024/05/01 13:15
http://poj.org/problem?id=1742
Coins
Time Limit: 3000MSMemory Limit: 30000KTotal Submissions: 22094Accepted: 7513

Description

People in Silverland usecoins.They have coins of value A1,A2,A3...An Silverland dollar.Oneday Tony opened his money-box and found there were some coins.Hedecided to buy a very nice watch in a nearby shop. He wanted to paythe exact price(without change) and he known the price would notmore than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An andC1,C2,C3...Cn corresponding to the number of Tony's coins of valueA1,A2,A3...An then calculate how many prices(form 1 to m) Tony canpay use these coins.

Input

The input contains several testcases. The first line of each test case contains two integersn(1<=n<=100),m(m<=100000).Thesecond line contains 2n integers, denotingA1,A2,A3...An,C1,C2,C3...Cn(1<=Ai<=100000,1<=Ci<=1000).The last test case is followed by two zeros.

Output

For each test case output theanswer on a single line.

Sample Input

3 101 2 4 2 1 12 51 4 2 10 0

Sample Output

84

Source

LouTiancheng@POJ
 
 
我去上课了。。方法1不大会刚学的。。这种方法完全脱离了常规的多重背包模版 ACM牛人真是太多了。。。方法2还有一个问题就是 multiplepack(A[i],A[i],C[i]);这个地方不理解。。
代码1(新方法)

#include<stdio.h>
#include<string.h>
#define maxn 105
#define maxm 100005
bool f[maxm];
int used[maxm], num[maxn], value[maxn];
int n, m;
void input() {
       int i;
   for (i = 0; i < n; i++)
           scanf("%d", &value[i]);
  for (i = 0; i < n; i++)
           scanf("%d", &num[i]);
}
void work() {
        memset(f, 0, sizeof(f));
 f[0] = true;
     int sum = 0;
     for (int i = 0; i < n; i++) {
             memset(used, 0, sizeof(used));//初始化存放使用次数  
             for (int j = value[i]; j <= m; j++)
                       if (!f[j] && f[j - value[i]] && used[j - value[i]] < num[i])//使用次数不可超过num 
                       {
                                f[j] = true;
                             used[j] = used[j - value[i]] + 1;//该物品使用次数增加  
                          sum++;//边背包边累计结果
                 }
        }
        printf("%d\n", sum);
}
int main() {
  while (scanf("%d %d", &n, &m), n | m) {
          input();
         work();
  }
        return 0;
}
代码2(居然不会超时) 
#include<stdio.h>
#include<string.h>
#define maxn 110
#define maxncash 1000010
int n, m, f[maxncash], A[maxn], C[maxn];
void zero(int cost, int weight) {
    int i;
   for (i = m; i >= cost; i--) //逆续
          if (f[i] < f[i - cost] + weight)
                  f[i] = f[i - cost] + weight;
}
void complepack(int cost, int weight) {
       int i;
   for (i = cost; i <= m; i++)
               if (f[i] < f[i - cost] + weight)//顺序
                      f[i] = f[i - cost] + weight;
}
void multiplepack(int cost, int weight, int account) {
        int k;
   if (account * weight >= m)
                complepack(cost, weight);
        else {
           k = 1;
           while (k < account) {
                     zero(k * cost, k * weight);
                      account -= k;
                    k *= 2;
          }
                zero(account * cost, account * weight);
  }
}
int main() {
     while (scanf("%d %d", &n, &m) != EOF && (n != 0 || m != 0)) {
            int i;
           int ans = 0;
             for (i = 1; i <= n; i++)
                  scanf("%d", &A[i]);//输入硬币的价值;
            for (i = 1; i <= n; i++)
                  scanf("%d", &C[i]);//输入对应硬币的价值;
          memset(f, 0, sizeof(f));//数组清零;
          for (i = 1; i <= n; i++)
                  multiplepack(A[i], A[i], C[i]);
          ans = 0;
         for (i = 1; i <= m; i++)
                  if (f[i] == i)
                           ans++;
           printf("%d\n", ans);
     }
        return 0;
}