分支界定是一种在问题的解空间树上搜索问题的解的方法,其实就是剪枝广搜,它始终维护一个上下界用来剪枝,一个限界函数计算对解的最有期望。主要用于解决离散问题的优化。


分支界定的关键问题:

(1)如何确定合适的限界函数

(2)如何组织待处理节点表

(3)如何记录最优解路径


限界函数应该是经过该节点所有搜索路径的最佳期望


最大问题:本质还是广搜,复杂度肯定不低,2^n的节点空间也受不了,试着解决01背包超时又超存,肯定不如DP,也可以解决TSP,排线问题等,


下边自己写的一个01背包,问题一堆,哎,菜鸟一个....

http://acm.hdu.edu.cn/showproblem.php?pid=2602

 

  1 #include<stdio.h>
  2 #include<queue>
  3 #include<algorithm>
  4 
  5 using namespace std;
  6 
  7 #define MAXN 1005
  8 
  9 struct ITEM
 10 {
 11     int w,v;
 12 }b[MAXN];
 13 
 14 struct NODE
 15 {
 16     int w,v,ve,pos;
 17 
 18     bool operator < (const NODE & other)const
 19     {
 20         return ve < other.ve;
 21     }
 22 };
 23 
 24 int search(ITEM * b,int num,int vol);
 25 int cmp(const void * a,const void * b);
 26 int expect(NODE a,ITEM * b,int num);
 27 
 28 int main()
 29 {
 30     //freopen("Sample Input.txt","r",stdin);
 31     int caseNum;
 32     scanf("%d",&caseNum);
 33 
 34     while(caseNum--)
 35     {
 36         int vol,num;
 37         scanf("%d%d",&num,&vol);
 38 
 39         for(int i = 0;i < num;i++)
 40         {
 41             scanf("%d",&b[i].v);
 42         }
 43         for(int i = 0;i < num;i++)
 44         {
 45             scanf("%d",&b[i].w);
 46         }
 47 
 48         printf("%d\n",search(b,num,vol));
 49     }
 50 }
 51 
 52 int search(ITEM * b,int num,int vol)
 53 {
 54     qsort(b,num,sizeof(b[0]),cmp);
 55 
 56     /*for(int i = 0;i < num;i++)
 57     {
 58         printf("%d %d\n",b[i].w,b[i].v);
 59     }*/
 60 
 61     int min = 0;                        //贪心的一个最小界
 62     int t = vol;
 63     for(int i = 0;i < num;i++)
 64     {
 65         if(t >= b[i].w)
 66         {
 67             min += b[i].v;
 68             t -= b[i].w;
 69         }
 70     }
 71     /*printf("%d %d\n",vol,min);*/
 72     priority_queue<NODE> que;
 73     
 74     NODE fir;
 75     fir.v = 0;
 76     fir.w = vol;
 77     fir.pos = 0;
 78     fir.ve = expect(fir,b,num);
 79     que.push(fir);
 80 
 81     while(!que.empty())
 82     {
 83         NODE cur = que.top();
 84         que.pop();
 85 
 86         if(cur.pos == num)
 87         {
 88             return cur.v;
 89         }
 90 
 91         for(int i = 0;i <= 1;i++)
 92         {
 93             NODE next = cur;
 94             if(i == 1 && next.w < b[cur.pos].w)
 95             {
 96                 continue;
 97             }
 98             next.w -= (i * b[cur.pos].w);
 99             next.v += (i * b[cur.pos].v);
100             next.pos++;
101             next.ve = expect(next,b,num);
102 
103             if(next.pos == num && next.v > min)
104             {
105                 min = next.v;
106             }
107             if(next.ve >= min)
108             {
109                 que.push(next);
110             }
111         }
112     }
113 }
114 
115 int cmp(const void * a,const void * b)
116 {
117     double rate_a = double(((ITEM *) a) -> v) / ((ITEM *) a) -> w;
118     double rate_b = double(((ITEM *) b) -> v) / ((ITEM *) b) -> w;
119     return rate_b - rate_a;
120 }
121 
122 int expect(NODE a,ITEM * b,int num)                        //限界函数
123 {
124     if(a.pos == num)
125     {
126         return a.v;
127     }
128     else
129     {
130         return a.v + a.w * b[a.pos].v / b[a.pos].w;
131     }
132 }

 

 posted on 2013-07-03 11:37  莫扎特的代码  阅读(391)  评论(0编辑  收藏  举报