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.
tongxun/分支限界法算法设计.cpp

56 lines
1.8 KiB

1 year ago
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
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]<5D><EFBFBD><E6B4A2><EFBFBD>Ǵӵ<C7B4>1<EFBFBD><31><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD>i<EFBFBD><69><EFBFBD><EFBFBD>Ʒ<EFBFBD>ļ<EFBFBD>ֵ<EFBFBD>ܺ<EFBFBD>
struct good{
int idx,c,r,tv; // idx<64><78>ʾѡ<CABE><D1A1><EFBFBD>±꣬c<EAA3AC><63>ʾ<EFBFBD><CABE>ѡ<EFBFBD><D1A1><EFBFBD>ĵ<EFBFBD>ǰ<EFBFBD>ܼ<EFBFBD>ֵ<EFBFBD><D6B5>r<EFBFBD><72>ʾ<EFBFBD><CABE>ǰѡ<C7B0><D1A1><EFBFBD><EFBFBD>ʣ<EFBFBD><CAA3><EFBFBD>ܼ<EFBFBD>ֵ<EFBFBD><D6B5>tv<74><76>ʾ<EFBFBD><CABE>ѡ<EFBFBD><D1A1><EFBFBD>ĵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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<good,vector<good>,greater<good>> 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) { // <20><><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>ӽڵ㣬<DAB5><E3A3AC><EFBFBD><EFBFBD><EFBFBD>´<EFBFBD><C2B4><EFBFBD>
res = max(res,t.c);
continue;
}
if(goods[idx].tv <= m && goods[idx].c + goods[idx].r > res) q.push(goods[idx]); // <20><><EFBFBD>統ǰѡ<C7B0><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>ǰ<EFBFBD><C7B0>ֵ+ʣ<><CAA3><EFBFBD><EFBFBD>ֵ > <20><>ǰ<EFBFBD><C7B0><EFBFBD>Ž⣬<C5BD><E2A3AC>װ<EFBFBD><EFBFBD><EBB1B3>
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;
}