Loading Problem Solving backtracking

Problem Description

N containers have to be loaded on a ship deadweight W, wherein the container i (1≤i≤n) the weight wi. Is not considered to limit the volume of the container, it is selected from the containers and the weight W or less and a plurality of vessels mounted on as large as possible.
For example, n = 5, W = 10 , w = {5,2,6,4,3} time, the best solution is loaded (1,1,0,0,1) or (0,0,1, 1,0), maxw = 10.

Problem Solving

With pruning solved by backtracking. Problem as follows:
int W [] = {0,5,2,6,4,3}; // weight of each of the containers, without the elements has 0
int n = 5, W = 10 ;
the results are shown below Solution :
int maxW = 0; // store the total weight of the optimal solution
int x [MAXN]; // store the optimal solution vector
to said data is designed to global variables.
Algorithm as follows:
void DFS (int i, int TW, int RW, int OP [])
where the parameter i represents the container i considered, tw represents the weight of containers selected and, rw denotes the weight of the remaining containers, and (initially when all and container weight), OP represents a solution, i.e., corresponding to a loading program.
The optimal solution: x, maxw
left pruning: only the left child node expansion tw + w [i] ≤W of
the right pruning: only extended tw + rw-w [i] > the right child node maxW

Code

int w[] = { 0,5,2,6,4,3 };
int n = 5, W = 10;
int maxw = 0;
int x[MAX];//存放最优解

void dfs(int i, int tw, int rw, int op[])
{
	if (i > n)
	{
		if (tw > maxw&&tw <= W)
		{
			maxw = tw;
			for (int j = 1; j <= n; j++)
				x[j] = op[j];
		}
	}
	else
	{
		if (tw + w[i] <= W)//左剪枝
		{
			op[i] = 1;//选择货物
			dfs(i + 1, tw + w[i], rw - w[i], op);
		}
		if (tw + rw - w[i] > maxw)//右剪枝
		{
			op[i] = 0;//不选择货物
			dfs(i + 1, tw, rw - w[i], op);
		}
	}
}

Solving complex problems loading

Problem Description

There are a total of n number of containers to be loaded on two load c1 and c2 respectively of the ship, where the weight of the container i is Wi, and w1 + w2 + ... + wn≤c1 + c2.
Loaded question asked to determine whether there is a reasonable program can load these containers loaded on these two ships. If so, find a loading program.
For example:
n-3 =, C1 = C2 = 50, W = {10,40,40} when, containers 1 and 2 may be attached to a ship on the first, and the second container 3 is attached to the ship calls.
n = 3, c1 = c2 = 50, w = {20,40,40}, these three containers can not be all installed on the ship.

Problem Solving

If a load given complex problem has a solution, can be used as a way to get the loading solution:
First, the first as a ship filled, then the remaining second container mounted on the ship calls.
You can be required to prove its correctness.
If you do not get a loading program, indicating that the complex loading problem is no solution!
Algorithm thinking (entered as W1, W2, ..., Wn, c1, c2):
(1) as many containers loaded on a ship to obtain the solution vector x.
(2) a total of Bahrain container ship remaining weight sum.
(3) if the sum <= c2, may represent a second ship calls Bahrain, returns true; otherwise not Bahrain shows a second ship calls, returns false.

Code

int w[] = { 0,10,40,40 };
int n = 3;
int c1 = 50, c2 = 50;
int maxw = 0;//存放第一艘轮船的最优总重量
int x[MAX];//存放第一艘轮船的最优解向量

void dfs(int i, int tw, int rw, int op[])
{
	if (i > n)
	{
		if (tw > maxw&&tw <= c1)
		{
			maxw = tw;
			for (int j = 1; j <= n; j++)
				x[j] = op[j];
		}
	}
	else
	{
		if (tw + w[i] <= c1)//左剪枝
		{
			op[i] = 1;//选择货物
			dfs(i + 1, tw + w[i], rw - w[i], op);
		}
		if (tw + rw - w[i] > maxw)//右剪枝
		{
			op[i] = 0;//不选择货物
			dfs(i + 1, tw, rw - w[i], op);
		}
	}
}

bool solve()
{
	int sum = 0;
	for (int j = 1; j <= n; j++)
		if (x[j] == 0)
			sum += w[j];//计算剩余货物的总重量
	if (sum <= c2)
		return true;
	else
		return false;
}
Published 26 original articles · won praise 0 · Views 293

Guess you like

Origin blog.csdn.net/weixin_42729072/article/details/104905407