题意 : 大体积 大容量 01 背包
题解 : 我们一般看到的01背包都是体积小或者是容量非常小的。我们有两种dp 方式 可以dp 最大价值,也可以dp 某个价值的最小体积,这两种 dp 的前提都是 时间复杂度 可以接受 O (N * V) || O (N * W) 的,但是这个题目熙然接受不了这种复杂度。01背包的题目一般情况下数据范围会提醒我们思考的方向,如果这两种方法都接受不了的话我们就考虑搜索剪枝 (就是乱搞) 因为之歌题 n 只有 100 是很可能过的 hhh
先按照性价比排序 然后对于一 每一个状态进行可行性剪枝和最优化剪枝。
怎么进行可行性剪枝呢 ?
剩余空间小于物品体积时直接剪枝;
怎么进行最优化剪枝呢 ?
由于经过了排序所以当当前物品的性价比乘以剩余体积仍然小于答案的时候就可以进行最优化剪枝直接剪枝掉这种情况就可以了。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
const int maxn = 1005;
struct node {
ll w,v;
}a[maxn];
ll ans = 0;
ll n,V;
void dfs (int k,ll sum,ll res) {
if (k == n + 1) {
ans = max (ans , sum);
return ;
}
if (res >= a[k].w) {
if (sum + res * a[k].v / double (a[k].w) < ans) return;
dfs (k + 1,sum + a[k].v,res - a[k].w);
}
dfs (k + 1,sum,res);
return ;
}
bool cmp (const node x,const node y) {
return x.v * y .w >= x.w * y .v;
}
int main () {
ios_base :: sync_with_stdio(false);
while (cin >> n >> V) {
for (int i = 1;i <= n; ++ i) {
cin >> a[i].w >> a[i].v;
}
sort (a + 1,a + n + 1,cmp);
ans = 0;
dfs (1,0,V);
cout << ans << endl;
}
return 0;
}