博客
关于我
AcWing 9. 分组背包问题(动态规划dp)
阅读量:351 次
发布时间:2019-03-04

本文共 2956 字,大约阅读时间需要 9 分钟。

有 N 组物品和一个容量是 V 的背包。

每组物品有若干个,同一组内的物品最多只能选一个。

每件物品的体积是 vij,价值是 wij,其中 ii 是组号,j 是组内编号。

求解将哪些物品装入背包,可使物品总体积不超过背包容量,且总价值最大。

输出最大价值。

输入格式

第一行有两个整数 N,V,V,用空格隔开,分别表示物品组数和背包容量。

接下来有 N 组数据:

  • 每组数据第一行有一个整数 Si,表示第 ii 个物品组的物品数量;
  • 每组数据接下来有 Si 行,每行有两个整数 vij,wij,用空格隔开,分别表示第 i 个物品组的第 j 个物品的体积和价值;

输出格式

输出一个整数,表示最大价值。

数据范围

0<N,V≤1000<N,V≤100

0<Si≤1000<Si≤100
0<vij,wij≤1000<vij,wij≤100

输入样例

3 521 22 413 414 5

输出样例:

8

思想:类似于多重背包问题,多重背包是可以选第i个物品至多s[i]件,此题只允许选一件,每件体积和重量不同。

最终还是转化为01背包问题,选不选第x个的问题

朴素版本:

import java.io.*;import java.lang.*;class Main{        static int n = 0, m = 0, N = 110;    static int[][] f = new int[N][N];    static int[][] v= new int[N][N], w = new int[N][N];    static int[] s = new int[N];        public static void main(String[] args)throws Exception{        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));        String[] params = buf.readLine().split(" ");        n = Integer.valueOf(params[0]);        m = Integer.valueOf(params[1]);                for(int i = 1; i <= n; ++i){            int cnt = Integer.valueOf(buf.readLine());            s[i] = cnt;//记录每组的数量            for(int j = 1; j <= cnt; ++j){                String[] info = buf.readLine().split(" ");                int a = Integer.valueOf(info[0]);                int b = Integer.valueOf(info[1]);                v[i][j] = a;                w[i][j] = b;            }        }                for(int i = 1; i <= n; ++i){            for(int j = 0; j <= m; ++j){                f[i][j] = f[i - 1][j];                for(int x = 0; x <= s[i]; ++x){                    if(v[i][x] <= j)                        f[i][j] = Math.max(f[i][j], f[i - 1][j - v[i][x]] + w[i][x]);                }            }        }        System.out.print(f[n][m]);    }}

优化版本;

import java.io.*;import java.lang.*;class Main{        static int n = 0, m = 0, N = 110;    static int[] f = new int[N];    static int[][] v= new int[N][N], w = new int[N][N];    static int[] s = new int[N];        public static void main(String[] args)throws Exception{        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));        String[] params = buf.readLine().split(" ");        n = Integer.valueOf(params[0]);        m = Integer.valueOf(params[1]);                for(int i = 1; i <= n; ++i){            int cnt = Integer.valueOf(buf.readLine());            s[i] = cnt;//记录每组的数量            for(int j = 1; j <= cnt; ++j){                String[] info = buf.readLine().split(" ");                int a = Integer.valueOf(info[0]);                int b = Integer.valueOf(info[1]);                v[i][j] = a;                w[i][j] = b;            }        }                for(int i = 1; i <= n; ++i){            for(int j = m; j >= 0; --j){                for(int x = 0; x <= s[i]; ++x){                    if(v[i][x] <= j)                        f[j] = Math.max(f[j], f[j - v[i][x]] + w[i][x]);                }            }        }        System.out.print(f[m]);    }}

 

转载地址:http://zyre.baihongyu.com/

你可能感兴趣的文章
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基本知识点梳理和查询优化
查看>>
mysql基础
查看>>
Mysql基础 —— 数据基础操作
查看>>
mysql基础---mysql查询机制
查看>>