一个比较简单的题。
题目大意
有n种配菜,每种有一个美味程度 和体积 你要选择一些配菜,使所选配菜的总体积不超过 W,并且美味程度尽可能大。
思路
看到这题的第一眼这不是 0/1 背包板子吗?然后看数据范围 ,但是 所以这题dfs爆搜就可以过。
时间复杂度 。
代码
#include<bits/stdc++.h>
#define push_back emplace_back
#define int long long
#define debug cout << "!" << '\n';
#define QwQ 0
using namespace std;
int n, w;
int a[30], b[30];
int ans = 0;
void dfs(int x, int cnt, int sum){ \\ x表示选到第x的位置, cnt表示目前所选的配菜的总体积, sum表示所选的配菜的美味度之和
if(cnt > w) return;
if(x > n){
ans = max(ans, sum);
return;
}
dfs(x + 1, cnt + b[x], sum + a[x]); \\选
dfs(x + 1, cnt, sum); \\不选
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> w;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
}
dfs(1, 0, 0);
cout << ans << '\n';
return QwQ;
}完结撒花!!!