多重背包及优化

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2191
这里写图片描述

多重背包及优化_第1张图片

一个简单的多重背包

使用1维数组

import java.util.Scanner;


/**
 * Created by hms on 2017/3/28.
 */
public class Main {
   public static void main(String[] args) {
         int T;
         Scanner scanner = new Scanner(System.in);
         T = scanner.nextInt(); //测试次数
         while ((T--) != 0) {
            int n, m;
            n = scanner.nextInt(); //经费
            m = scanner.nextInt(); //大米的种类

            int w[] = new int[100+5]; //存储每袋大米的价格
            int v[] = new int[100+5]; //存储每袋大米的重量
            int c[] = new int[100+5]; //每种大米有多少袋
            int dp[] = new int[105];

            for(int i = 1; i <= m; ++i) {
               w[i] = scanner.nextInt();
               v[i] = scanner.nextInt();
               c[i] = scanner.nextInt();
            }

            for(int i = 1; i <= m; ++i) {
               for(int j = 1; j <= c[i]; ++j) {
                  for(int k = n; k >= w[i]; --k) {
                        dp[k] = Math.max(dp[k], (dp[k-w[i]] + v[i]));
                  }
               }
            }

            System.out.println(dp[n]);
            /**
             * 清空局部变量表 ,及时Gc
             */
            w = null;
            v = null;
            c = null;
            dp = null;
         }
   }
}

使用2维数组

import java.util.Scanner;


/**
 * Created by hms on 2017/3/28.
 */
public class Main {
   public static void main(String[] args) {
         int T;
         Scanner scanner = new Scanner(System.in);
         T = scanner.nextInt(); //测试次数
         while ((T--) != 0) {
            int n, m;
            n = scanner.nextInt(); //经费
            m = scanner.nextInt(); //大米的种类

            int w[] = new int[100+5]; //存储每袋大米的价格
            int v[] = new int[100+5]; //存储每袋大米的重量
            int c[] = new int[100+5]; //每种大米有多少袋
            int dp[][] = new int[105][105];

            for(int i = 1; i <= m; ++i) {
               w[i] = scanner.nextInt();
               v[i] = scanner.nextInt();
               c[i] = scanner.nextInt();
            }

            for(int i = 1; i <= m; ++i) {
               for(int j = 1; j <= n; ++j) {

                  for(int k = 0; k <= c[i]; ++k) {
                     if(k * w[i] <= j) {
                        dp[i][j] =
                                Math.max(dp[i][j], dp[i - 1][j - k * w[i]] + k*v[i]);
                     } else {
                        break;
                     }
                  }
               }
            }

            System.out.println(dp[m][n]);
            /**
             * 清空局部变量表 ,及时Gc
             */
            w = null;
            v = null;
            c = null;
            dp = null;
         }
   }
}

二进制优化

import java.util.Scanner;
import java.util.Vector;


/**
 * Created by hms on 2017/3/28.
 */
public class Main {
   public static void main(String[] args) {
         int T;
         Scanner scanner = new Scanner(System.in);
         T = scanner.nextInt(); //测试次数
         while ((T--) != 0) {
            int n, m;
            n = scanner.nextInt(); //经费
            m = scanner.nextInt(); //大米的种类

            int w[] = new int[100+5]; //存储每袋大米的价格
            int v[] = new int[100+5]; //存储每袋大米的重量
            int c[] = new int[100+5]; //每种大米有多少袋
            int dp[] = new int[105];

            for(int i = 1; i <= m; ++i) {
               w[i] = scanner.nextInt();
               v[i] = scanner.nextInt();
               c[i] = scanner.nextInt();
            }

            for(int i = 1; i <= m; ++i) {
               int k = c[i];
               Vector vector = new Vector<>();

               for(int  j = 0; ; ++j) {
                  int x = (int)Math.pow(2, j);
                  if(x < k) {
                     vector.add(x);
                     k-=x;
                  } else {
                     vector.add(k);
                     break;
                  }
               }

               for(int p = 0; p < vector.size(); ++p) {
                  int factor = vector.get(p);

                  for(int j = n; j >= 0; --j) {
                     if(factor * w[i] <= j) {
                        dp[j] = Math.max(dp[j], dp[j-factor*w[i]] + factor * v[i]);
                     }
                  }
               }
            }

            System.out.println(dp[n]);

            /**
             * 清空局部变量表 ,及时Gc
             */
            w = null;
            v = null;
            c = null;
            dp = null;
         }
   }
}

你可能感兴趣的:(算法)