Problem Description
Some brave warriors come to a lost village. They are very lucky and find a lot of treasures and a big treasure chest, but with angry zombies.
The warriors are so brave that they decide to defeat the zombies and then bring all the treasures back. A brutal long-drawn-out battle lasts from morning to night and the warriors find the zombies are undead and invincible.
Of course, the treasures should not be left here. Unfortunately, the warriors cannot carry all the treasures by the treasure chest due to the limitation of the capacity of the chest. Indeed, there are only two types of treasures: emerald and sapphire. All of the emeralds are equal in size and value, and with infinite quantities. So are sapphires.
Being the priest of the warriors with the magic artifact: computer, and given the size of the chest, the value and size of each types of gem, you should compute the maximum value of treasures our warriors could bring back.
Input
There are multiple test cases. The number of test cases T (T <= 200) is given in the first line of the input file. For each test case, there is only one line containing five integers N, S1, V1, S2, V2, denoting the size of the treasure chest is N and the size and value of an emerald is S1 and V1, size and value of a sapphire is S2, V2. All integers are positive and fit in 32-bit signed integers.
Output
For each test case, output a single line containing the case number and the maximum total value of all items that the warriors can carry with the chest.
Sample Input
2
100 1 1 2 2
100 34 34 5 3
Sample Output
Case #1: 100
Case #2: 86
题目分析
max
Z
=
V
1
x
1
+
V
2
x
2
{
S
1
x
1
+
S
2
x
2
≤
N
x
1
≥
0
,
x
2
≥
0
x
1
和
x
2
都
是
整
数
\max Z=V_1x_1+V_2x_2\\ \left\{\begin{matrix} S_1x_1+S_2x_2\leq N\\ x_1\geq0,x_2\geq0\\ x_1和x_2都是整数 \end{matrix}\right.
maxZ=V1x1+V2x2⎩⎨⎧S1x1+S2x2≤Nx1≥0,x2≥0x1和x2都是整数
max
Z
=
x
1
+
2
x
2
{
x
1
+
2
x
2
≤
100
x
1
≥
0
,
x
2
≥
0
x
1
和
x
2
都
是
整
数
\max Z=x_1+2x_2\\ \left\{\begin{matrix} x_1+2x_2\leq 100\\ x_1\geq0,x_2\geq0\\ x_1和x_2都是整数 \end{matrix}\right.
maxZ=x1+2x2⎩⎨⎧x1+2x2≤100x1≥0,x2≥0x1和x2都是整数
松弛问题:
max
Z
=
x
1
+
2
x
2
{
x
1
+
2
x
2
≤
100
x
1
≥
0
,
x
2
≥
0
\max Z=x_1+2x_2\\ \left\{\begin{matrix} x_1+2x_2\leq 100\\ x_1\geq0,x_2\geq0\\ \end{matrix}\right.
maxZ=x1+2x2{x1+2x2≤100x1≥0,x2≥0
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | b b b |
---|---|---|---|---|
x 3 x_3 x3 | 1 | 2 | 1 | 100 |
λ j \lambda_j λj | 1 | 2 | 0 |
x 3 x_3 x3出基, x 2 x_2 x2进基,
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | b b b |
---|---|---|---|---|
x 3 x_3 x3 | 0.5 | 1 | 0.5 | 50 |
λ j \lambda_j λj | -1 | 0 | -1 |
则最优解为
(
x
1
,
x
2
)
=
(
0
,
50
)
(x_1,x_2)=(0,50)
(x1,x2)=(0,50),最优值为
2
×
50
=
100
2\times 50=100
2×50=100
max
Z
=
34
x
1
+
3
x
2
{
34
x
1
+
5
x
2
≤
100
x
1
≥
0
,
x
2
≥
0
x
1
和
x
2
都
是
整
数
\max Z=34x_1+3x_2\\ \left\{\begin{matrix} 34x_1+5x_2\leq 100\\ x_1\geq0,x_2\geq0\\ x_1和x_2都是整数 \end{matrix}\right.
maxZ=34x1+3x2⎩⎨⎧34x1+5x2≤100x1≥0,x2≥0x1和x2都是整数松弛问题:
max
Z
=
34
x
1
+
3
x
2
{
34
x
1
+
5
x
2
≤
100
x
1
≥
0
,
x
2
≥
0
\max Z=34x_1+3x_2\\ \left\{\begin{matrix} 34x_1+5x_2\leq 100\\ x_1\geq0,x_2\geq0\\ \end{matrix}\right.
maxZ=34x1+3x2{34x1+5x2≤100x1≥0,x2≥0
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | b b b |
---|---|---|---|---|
x 3 x_3 x3 | 34 | 5 | 1 | 100 |
λ j \lambda_j λj | 34 | 3 | 0 |
x 3 x_3 x3出基, x 1 x_1 x1进基,
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | b b b |
---|---|---|---|---|
x 3 x_3 x3 | 1 | 5 34 \frac{5}{34} 345 | 1 34 \frac{1}{34} 341 | 100 34 \frac{100}{34} 34100 |
λ j \lambda_j λj | 0 | -2 | -1 |
则最优解为
(
x
1
,
x
2
)
=
(
100
34
,
0
)
(x_1,x_2)=(\frac{100}{34},0)
(x1,x2)=(34100,0),不是整数解。
分支定界法:加入约束
x
1
≤
2
x_1\leq 2
x1≤2,因为
34
×
3
>
100
34\times3>100
34×3>100不满足第一条约束,所以约束
x
1
≥
3
x_1\geq3
x1≥3不考虑
max
Z
=
34
x
1
+
3
x
2
{
34
x
1
+
5
x
2
≤
100
x
1
≤
2
x
1
≥
0
,
x
2
≥
0
\max Z=34x_1+3x_2\\ \left\{\begin{matrix} 34x_1+5x_2\leq 100\\ x_1\leq2\\ x_1\geq0,x_2\geq0\\ \end{matrix}\right.
maxZ=34x1+3x2⎩⎨⎧34x1+5x2≤100x1≤2x1≥0,x2≥0
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | x 4 x_4 x4 | b b b |
---|---|---|---|---|---|
x 3 x_3 x3 | 34 | 5 | 1 | 0 | 100 |
x 4 x_4 x4 | 1 | 0 | 0 | 1 | 2 |
λ j \lambda_j λj | 34 | 3 | 0 | 0 |
x 4 x_4 x4出基, x 1 x_1 x1进基
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | x 4 x_4 x4 | b b b |
---|---|---|---|---|---|
x 3 x_3 x3 | 0 | 5 | 1 | -34 | 32 |
x 1 x_1 x1 | 1 | 0 | 0 | 1 | 2 |
λ j \lambda_j λj | 0 | 3 | 0 | -34 |
x 3 x_3 x3出基, x 2 x_2 x2进基
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | x 4 x_4 x4 | b b b |
---|---|---|---|---|---|
x 2 x_2 x2 | 0 | 1 | 1 5 \frac{1}{5} 51 | − 34 5 \frac{-34}{5} 5−34 | 32 5 \frac{32}{5} 532 |
x 1 x_1 x1 | 1 | 0 | 0 | 1 | 2 |
λ j \lambda_j λj | 0 | 0 | − 3 5 \frac{-3}{5} 5−3 | − 68 34 \frac{-68}{34} 34−68 |
则最优解为
(
x
1
,
x
2
)
=
(
2
,
32
5
)
(x_1,x_2)=(2,\frac{32}{5})
(x1,x2)=(2,532),不是整数解。
分支定界法:加入约束
x
2
≤
6
x_2\leq 6
x2≤6,因为
34
×
2
+
5
×
7
>
100
34\times2+5\times7>100
34×2+5×7>100不满足第一条约束,所以约束
x
2
≥
7
x_2\geq7
x2≥7不考虑
max
Z
=
34
x
1
+
3
x
2
{
34
x
1
+
5
x
2
≤
100
x
1
≤
2
x
2
≤
6
x
1
≥
0
,
x
2
≥
0
\max Z=34x_1+3x_2\\ \left\{\begin{matrix} 34x_1+5x_2\leq 100\\ x_1\leq2\\ x_2\leq 6\\ x_1\geq0,x_2\geq0\\ \end{matrix}\right.
maxZ=34x1+3x2⎩⎪⎪⎨⎪⎪⎧34x1+5x2≤100x1≤2x2≤6x1≥0,x2≥0
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | x 4 x_4 x4 | x 5 x_5 x5 | b b b |
---|---|---|---|---|---|---|
x 3 x_3 x3 | 34 | 5 | 1 | 0 | 0 | 100 |
x 4 x_4 x4 | 1 | 0 | 0 | 1 | 0 | 2 |
x 5 x_5 x5 | 0 | 1 | 0 | 0 | 1 | 6 |
λ j \lambda_j λj | 34 | 3 | 0 | 0 | 0 |
x 4 x_4 x4出基, x 1 x_1 x1进基
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | x 4 x_4 x4 | x 5 x_5 x5 | b b b |
---|---|---|---|---|---|---|
x 3 x_3 x3 | 0 | 5 | 1 | -34 | 0 | 32 |
x 1 x_1 x1 | 1 | 0 | 0 | 1 | 0 | 2 |
x 5 x_5 x5 | 0 | 1 | 0 | 1 | 1 | 6 |
λ j \lambda_j λj | 0 | 3 | 0 | -34 | 0 |
x 5 x_5 x5出基, x 2 x_2 x2进基
x B x_B xB | x 1 x_1 x1 | x 2 x_2 x2 | x 3 x_3 x3 | x 4 x_4 x4 | x 5 x_5 x5 | b b b |
---|---|---|---|---|---|---|
x 3 x_3 x3 | 0 | 0 | 1 | -39 | 0 | 2 |
x 1 x_1 x1 | 1 | 0 | 0 | 1 | 0 | 2 |
x 2 x_2 x2 | 0 | 1 | 0 | 1 | 1 | 6 |
λ j \lambda_j λj | 0 | 0 | 0 | -37 | -3 |
最优解为 ( x 1 , x 2 ) = ( 2 , 6 ) (x_1,x_2)=(2,6) (x1,x2)=(2,6),最优值为 34 × 2 + 3 × 6 = 68 + 18 = 86 34\times 2+3\times 6=68+18=86 34×2+3×6=68+18=86
单纯形法模板
上面是实现思路,代码有空来补。