基礎揹包問題 - 無界揹包問題或完全揹包問題 - 深度優先搜索 (遞歸)
1. 基礎揹包問題
有 N
種物品和一個承受最大重量爲 W
的揹包。第 i
種物品的重量是 w[i]
,價值是 v[i]
。求解將哪些物品裝入揹包可使這些物品的重量總和不超過揹包容量,且價值總和最大。
揹包問題 (Knapsack problem) 是一種組合優化的 NP 完全問題。給定一組物品,每種物品都有自己的重量和價格,在限定的總重量內,我們如何選擇,才能使得物品的總價格最高。
如果限定每種物品只能選擇 0 個或 1 個,則問題稱爲 0-1 揹包問題 (0-1 knapsack problem) 。
如果限定物品 j
最多隻能選擇 maxj
個,則問題稱爲有界揹包問題 (bounded knapsack problem,BKP)。
如果不限定每種物品的數量,每種物品都就可以選擇任意多個,則問題稱爲無界揹包問題或完全揹包問題 (unbounded knapsack problem,UKP)。
2. 無界揹包問題或完全揹包問題
有 N
種物品和一個容量是 V
的揹包,每種物品都有無限件可用。第 i
種物品的體積是 ,價值是 。求解將哪些物品裝入揹包,可使這些物品的總體積不超過揹包容量,且總價值最大,輸出最大價值 10。
輸入格式
2 // 測試用例個數
4 5 // N = 4 物品的類別數,V = 5 揹包的容量,最大價值 10
1 2 // v[0] = 1,w[0] = 2
2 4 // v[1] = 2,w[1] = 4
3 4 // v[2] = 3,w[2] = 4
4 5 // v[3] = 4,w[3] = 5
4 5 // N = 4 物品的類別數,V = 5 揹包的容量,最大價值 10
1 2
2 4
3 4
4 5
2 10 // N = 2 物品的類別數,V = 10 揹包的容量,最大價值 10
5 5
7 8
4 10 // N = 4 物品的類別數,V = 10 揹包的容量,最大價值 12
7 9
4 5
3 3
2 1
兩個整數 N
V
,用空格隔開,分別表示物品種數和揹包容量。接下來有 N
行,每行兩個整數 ,用空格隔開,分別表示第 i
種物品的體積和價值。 表示體積, 表示價值。
4
4 5
1 2
2 4
3 4
4 5
4 5
1 2
2 4
3 4
4 5
2 10
5 5
7 8
4 10
7 9
4 5
3 3
2 1
2.1 dfs_v1
/*
============================================================================
Name : yongqiang.cpp
Author : Yongqiang Cheng
Version : Version 1.0.0
Copyright : Copyright (c) 2020 Yongqiang Cheng
Description : Hello World in C++, Ansi-style
============================================================================
*/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <time.h>
using namespace std;
int computation = 0;
void dfs_v1(int map[100][2], int N, int V, int sumvolume, int sumvalue, int *max_value, int step)
{
if (sumvolume > V)
{
return;
}
if ((sumvalue > *max_value))
{
*max_value = sumvalue;
}
if (step >= N)
{
computation++;
return;
}
int addvolume = 0, addvalue = 0;
// 選這類物品,然後選下一類
dfs_v1(map, N, V, sumvolume + map[step][0], sumvalue + map[step][1], max_value, step + 1);
// 選這類物品,然後繼續選擇同類
dfs_v1(map, N, V, sumvolume + map[step][0], sumvalue + map[step][1], max_value, step);
// 不選這類物品,然後選下一類
dfs_v1(map, N, V, sumvolume, sumvalue, max_value, step + 1);
return;
}
int inference(int map[100][2], int N, int V)
{
int ret = 0;
int max_value = INT_MIN;
int step = 0;
int sumvolume = 0;
int sumvalue = 0;
dfs_v1(map, N, V, sumvolume, sumvalue, &max_value, step);
ret = max_value;
return ret;
}
int main()
{
clock_t start = 0, end = 0;
double cpu_time_used = 0;
const char input_txt[] = "D:\\visual_studio_workspace\\yongqiangcheng\\yongqiangcheng\\input.txt";
const char output_txt[] = "D:\\visual_studio_workspace\\yongqiangcheng\\yongqiangcheng\\output.txt";
freopen(input_txt, "r", stdin);
// freopen(output_txt, "w", stdout);
start = clock();
printf("Start of the program, start = %ld\n", start);
printf("Start of the program, start = %ld\n\n", start);
int case_num;
cin >> case_num;
for (int i = 1; i <= case_num; i++)
{
int ret = 0;
int N = 0, V = 0;
int map[100][2] = { 0 };
computation = 0;
cin >> N >> V;
for (int h = 0; h < N; h++)
{
cin >> map[h][0] >> map[h][1]; // volume - value
}
ret = inference(map, N, V);
cout << "computation " << i << " = " << computation << endl;
cout << "CASE #" << i << " = " << ret << endl;
}
end = clock();
printf("\nEnd of the program, end = %ld\n", end);
printf("End of the program, end = %ld\n", end);
cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %f\n", cpu_time_used);
printf("Exiting of the program...\n");
fclose(stdin);
// fclose(stdout);
return 0;
}
Start of the program, start = 0
Start of the program, start = 0
computation 1 = 51
CASE #1 = 10
computation 2 = 51
CASE #2 = 10
computation 3 = 7
CASE #3 = 10
computation 4 = 83
CASE #4 = 12
End of the program, end = 6
End of the program, end = 6
Total time taken by CPU: 0.006000
Exiting of the program...
請按任意鍵繼續. . .