From 9f8bcffb02c4ea98f60d0a4a5ae7627fe1472218 Mon Sep 17 00:00:00 2001 From: liujun <2575422181@qq.com> Date: Sat, 28 Oct 2023 16:36:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 分支限界法算法设计.cpp | 55 +++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 分支限界法算法设计.cpp diff --git a/分支限界法算法设计.cpp b/分支限界法算法设计.cpp new file mode 100644 index 0000000..ac94714 --- /dev/null +++ b/分支限界法算法设计.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +const int N = (1 << 20) + 10; +int n,m; +int v[N],w[N]; +int res = -1; +int s[N]; // s[i]洢Ǵӵ1ƷiƷļֵܺ + +struct good{ + int idx,c,r,tv; // idxʾѡ±꣬cʾѡĵǰֵܼrʾǰѡʣֵܼtvʾѡĵǰ + bool operator > (const good& W) const{ + return W.c + W.r > c + r; + } +}goods[N]; + +int bfs(){ + goods[1] = {1,0,0,0}; +priority_queue,greater> q; + q.push(goods[1]); + + while(q.size()){ + auto t = q.top(); +// cout << t.idx << endl; + q.pop(); + int idx = t.idx << 1; + goods[idx] = {idx,goods[t.idx].c,s[n] - s[(int)log2(idx)],goods[t.idx].tv}; + goods[idx + 1] = {idx + 1,goods[t.idx].c + w[(int)log2(idx)],s[n] - s[(int)log2(idx)],goods[t.idx].tv + v[(int)log2(idx)]}; + if((int)log2(t.idx) == n) { // Ѿӽڵ㣬´ + res = max(res,t.c); + continue; + } + + if(goods[idx].tv <= m && goods[idx].c + goods[idx].r > res) q.push(goods[idx]); // 統ǰѡҵǰֵ+ʣֵ > ǰŽ⣬װ뱳 + if(goods[idx + 1].tv <= m && goods[idx + 1].c + goods[idx + 1].r > res) q.push(goods[idx + 1]); + } + + return res; +} + +int main(){ + cin >> n >> m; + for(int i = 1; i <= n; i ++) cin >> v[i] >> w[i],s[i] = s[i - 1] + w[i]; +// +// for(int i = 2; i < 1 << n + 1; i ++){ +// goods[i] = {i,goods[i >> 1].c + (i&1)*w[(int)log2(i)],s[n] - s[(int)log2(i)],goods[i >> 1].tv + (i&1)*v[(int)log2(i)]}; +// } +// +// for(int i = 1; i < 1 << n + 1; i ++) printf("i = %d,c[i] = %d,r[i] = %d,tv[i] = %d\n",goods[i].idx,goods[i].c,goods[i].r,goods[i].tv); + cout << bfs() << endl; + return 0; +}