分支限界法之01背包优队列版

代码:

  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <iostream>
  4 #include <queue>
  5 #include <vector>
  6 using namespace std;
  7 
  8 const int maxn=1000;
  9 
 10 class node
 11 {
 12 public:
 13     int id,w,p;
 14     double average;
 15     bool operator < (const node &a)const
 16     {
 17         return average>a.average;
 18     };
 19 }ths[maxn];
 20 
 21 class bbnode
 22 {
 23 public:
 24     bbnode *parent;
 25     bool lchild;
 26     bbnode(bbnode *b,bool f)
 27     {
 28         parent=b;
 29         lchild=f;
 30     };
 31     bbnode(const bbnode &a)
 32     {
 33         parent=a.parent;
 34         lchild=a.lchild;
 35     };
 36 };
 37 
 38 class headnode
 39 {
 40 public:
 41     double up;
 42     int cp,cw;
 43     int level;
 44     bbnode *bb;
 45     headnode(double u,int p,int w,int l,bbnode b)
 46     {
 47         up=u;
 48         cp=p;
 49         cw=w;
 50         level=l;
 51         bbnode *yy=new bbnode(b.parent,b.lchild);
 52         bb=yy;
 53     }
 54 };
 55 
 56 int n;
 57 int c;
 58 int cw;
 59 int cp;
 60 int bestp;
 61 int best[maxn];
 62 
 63 bool cmpp(node a,node b)
 64 {
 65     return a.id<b.id;
 66 }
 67 double bound(int num)
 68 {
 69     int cleft=c-cw;
 70     double p=cp;
 71     while(num<=n && ths[num].w <= cleft)
 72     {
 73         cleft-=ths[num].w;
 74         p+=ths[num].p;
 75         num++;
 76     }
 77     if(num<=n)
 78     {
 79         p+=1.0*cleft/ths[num].w * ths[num].p;
 80     }
 81     return p;
 82 }
 83 
 84 
 85 void init()
 86 {
 87     cw=0;
 88     cp=0;
 89     bestp=0;
 90 }
 91 
 92 int bfs()
 93 {
 94     queue<headnode> que;
 95     bbnode *E;
 96     bbnode *beste;
 97     E=0;
 98     beste=0;
 99     int i=1;
100     int t=bound(1);
101     while(i<=n)
102     {
103         if(cw+ths[i].w<=c)
104         {
105             if(cp+ths[i].p>bestp)
106             {
107                 bestp=cp+ths[i].p;
108                 beste=E;
109             }
110             que.push(headnode(t,cp+ths[i].p,cw+ths[i].w,i+1,bbnode(E,true)));
111         }
112         t=bound(i+1);
113         if(t>=bestp)
114             que.push(headnode(t,cp,cw,i+1,bbnode(E,false)));
115         headnode now=que.front();
116         que.pop();
117         cp=now.cp;
118         cw=now.cw;
119         i=now.level;
120         t=now.up;
121         E=now.bb;
122     }
123     if(bestp>=cp)
124         beste=E;
125     for(int j=n;j>0;j--)
126     {
127         best[j]= beste->lchild;
128         beste=beste->parent;
129     }
130     return bestp;
131 }
132 
133 int main()
134 {
135     cin>>n>>c;
136     for(int i=1;i<=n;i++)
137     {
138         cin>>ths[i].p;
139         ths[i].id=i;
140     }
141     for(int i=1;i<=n;i++)
142     {
143         cin>>ths[i].w;
144         ths[i].id=i;
145         ths[i].average=1.0*ths[i].p/ths[i].w;
146     }
147     sort(ths+1,ths+n+1);
148     init();
149     cout<<bfs()<<endl;
150     vector<node> v;
151     for(int i=1;i<=n;i++)
152         if(best[i])
153             v.push_back(ths[i]);
154     sort(v.begin(),v.end(),cmpp);
155     for(int i=0;i<v.size();i++)
156         cout<<"The "<<v[i].id<<"th : "<<"p="<<v[i].p<<" w="<<v[i].w<<endl;
157     return 0;
158 }

输入示例1:

4 7

9 10 7 4

3 5 2 1

输出示例1:

20

The 1th : p=9 w=3

The 3th : p=7 w=2

The 4th : p=4 w=1

输入示例2:

6 12

8 10 6 3 7 2

4 6 2 2 5 1

输出示例2:

24

The 1th : p=8 w=4

The 2th : p=10 w=6

The 3th : p=6 w=2

猜你喜欢

转载自www.cnblogs.com/wangxuelin/p/9048475.html