⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beibao.c

📁 采用分枝限界法解决0/1背包问题! 本人上机实习作业,通过老师验收,合格! 针对部分上机实习的同学可以来下~
💻 C
字号:

#include<stdio.h>
#include<stdlib.h>
#define null 0
int P[5]={0,10,10,12,18};
int W[5]={0,2,4,6,9};
/*int N=4;
int M=15;*/
int LBB=0,UBB=0;


struct node
{
	int mark;
	int Parent;
	int Lever;
	int Tag;
	int CU;
	int PE;
	int UB;
	struct node *next;
};
struct node *head,*ANS,*newhead;




LUBOUND(int rw,int cp,int N,int K);//计算下界和上界的算法
NEWNODE(int par,int lev,int t,int cap,int prof,int ub,int mark);//生成一个新结点
struct node *LARGEST();//在活结点表中取一个具有最大UB值的结点作为下一个E-结点
FINISH(int L,int N);//打印答案
struct node *SEARCH(int m);//寻找满足题意的结点
INSERT(struct node *node);//将E-结点插入活结点表中
int MAX(int m,int n);//取2数的最大值
insert(struct node *node);//生成结果结点表

//LC分枝—限界算法
LCKNAP(int M,int N,int e)
{
	int L,cap,prof;
	int i,mark=0;
	struct node *p;
	p=(struct node *)malloc(sizeof(struct node));
	p->mark=0;
	p->Parent=0;
	p->Lever=1;
	p->CU=M;
	p->PE=0;
	LUBOUND(M,0,N,1);
	L=LBB-e;
	p->UB=UBB;
	while(p->UB>=L)
	{
		insert(p);
		i=p->Lever;
		cap=p->CU;
		prof=p->PE;
		while(1)
		{
		if(i==N+1)
		{
			if(prof>L)
				 {
					 L=prof;
				 }
					 ANS=p;

			break;
		}
		else
		{
			if(cap>=W[i])
			{
				mark++;
				NEWNODE(p->mark,i+1,1,cap-W[i],prof+P[i],p->UB,mark);
			}
			LUBOUND(cap,prof,N,i+1);
			if(UBB>L)
			{
				mark++;
				NEWNODE(p->mark,i+1,0,cap,prof,UBB,mark);
				L=MAX(L,LBB-e);
			}
		}
		break;
		}
		if(head==null)
			break;
		else p=LARGEST(head);
	}
	FINISH(L,N);
}

int MAX(int m,int n)
{
	if(m>n) return m;
	else return n;
}


LUBOUND(int rw,int cp,int N,int k)
{
	int i,j,c;
	LBB=cp;
	c=rw;
	for(i=k;i<=N;i++)
	{
		if(c<W[i])
		{
			UBB=LBB+c*P[i]/W[i];
			for(j=i+1;j<=N;j++)
			{

				if(c>=W[j])
				{
					c=c-W[j];
					LBB=LBB+P[j];
				}
			}
			return 0;
		}
			c=c-W[i];
		LBB=LBB+P[i];
	}
	UBB=LBB;
	return 0;
}


NEWNODE(int par,int lev,int t,int cap,int prof,int ub,int mark)
{
	struct node *newnode;
	newnode=(struct node *)malloc(sizeof(struct node));
	newnode->Parent=par;
	newnode->Lever=lev;
	newnode->Tag=t;
	newnode->CU=cap;
	newnode->PE=prof;
	newnode->UB=ub;
	newnode->mark=mark;
	INSERT(newnode);
}

INSERT(struct node *node)
{
	struct node *p,*q;
	p=head;
	if(head==null)
	{
		head=node;
		node->next=null;
	}
	else
	{
		while((p->UB>=node->UB)&&(p->next!=null))
		{
			q=p;
			p=p->next;
		}
		if(p->UB<node->UB)
		{
			if(head==p)
			{
				node->next=head;
				head=node;
			}
			else
			{
				q->next=node;
				node->next=p;
			}

		}
		else
		{
			p->next=node;
			node->next=null;
		}
	}
}


insert(struct node *node)
{
	struct node *p;
	p=newhead;
	if(newhead==null)
	{
		newhead=node;
		node->next=null;
	}
	else
	{
		newhead=node;
		node->next=p;
	}
}

struct node *LARGEST()
{
	struct node *node;
	node=head;
	head=head->next;
	return node;

}


FINISH(int L,int N)
{
	int i,j,m=3;
	int result[4];
    printf("最优解的效益值为:%d\n",L);
	for(j=N;j>=1;j--)
	{
		result[m]=ANS->Tag;
		m--;
		i=ANS->Parent;
		ANS=SEARCH(i);
	}
	printf("最优解选择是:");
	for(i=0,j=1;i<4;i++,j++)
	{
		if(result[i]==1)
		{
			printf("%d  ",j);
		}
	}
	printf("号物品!\n");

}

struct node *SEARCH(int m)
{
	struct node *p;
	p=newhead;
	while(p!=null)
	{
		if(p->mark==m)
		{
			return p;
		}
		else p=p->next;
	}
	return null;
}



void main()
{
	LCKNAP(15,4,0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -