You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
// https://blog.csdn.net/gnn8291/article/details/90632100
using namespace std ;
int W ; // 可以提起 W单位重量的东西,
int V ; // 有一个能装V个单位体积的购物袋
int n ; // 为商品种类数
// 注意多维限制的01背包, 一般N值都比较小, 100左右, 太大就会编译出错了。
const int N = 110 ;
// 某种商品的重量、体积和让利金额
int a [ N ] , b [ N ] , c [ N ] ;
// DP数组
int f [ N ] [ N ] [ N ] ;
/**
10 9 4
8 3 6
5 4 5
3 7 7
4 5 4
答案:
9
2 4
*/
int main ( ) {
// 输入
cin > > W > > V > > n ;
// 每一种商品的重量、体积和让利金额
for ( int i = 1 ; i < = n ; i + + ) cin > > a [ i ] > > b [ i ] > > c [ i ] ;
// 遍历每个种类
for ( int i = 1 ; i < = n ; i + + ) {
for ( int j = 1 ; j < = W ; j + + ) { // 遍历重量
for ( int k = 1 ; k < = V ; k + + ) { // 遍历体积
int x = f [ i - 1 ] [ j - a [ i ] ] [ k - b [ i ] ] + c [ i ] ; // 如果选了, 结果是x
int y = f [ i - 1 ] [ j ] [ k ] ; // 如果不选, 结果是y
// 如果剩余的重量和体积都够用的时候,尝试拿当前物品
if ( j > = a [ i ] & & k > = b [ i ] )
f [ i ] [ j ] [ k ] = max ( x , y ) ; // 也是两个分支,一是不选,另一个是选,要取最大值
else
f [ i ] [ j ] [ k ] = y ; // 装不下,不管是哪种原因装不下,都是这个分支
}
}
}
// 输出
cout < < f [ n ] [ W ] [ V ] < < endl ; // 小惠能够得到的最大让利金额
return 0 ;
}