作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main () {
string line;
int weightCapacity;
int numItems;
double weight, value;
map<double,int> set;
vector<int> w;
vector<int> v;
vector<int> knapsack;
int bestValue = 0;
int subsetValue = 0;
int subsetWeight = 0;
int i;
vector<int> items;
ifstream myFile ("knapsack.txt");
// reading a text file
if (myFile.is_open()) {
cout << "Open" << endl;
}
else cout << "Unable to open file";
myFile >> weightCapacity;
myFile >> numItems;
cout << weightCapacity << endl;
cout << numItems << endl;
for (i = 0; i < numItems; i++) {
myFile >> weight;
myFile >> value;
set[value/weight] = i;
w.push_back(weight);
v.push_back(value);
cout << weight << " " << value << endl;
}
int currentW = weightCapacity;
int totalValue = 0;
for (auto iter = set.rbegin(); iter != set.rend(); ++iter) {
cout << iter->first << ": " << iter->second << endl;
int W = w.at(iter->second);
if (W <= currentW) {
items.push_back(iter->second);
totalValue += v.at(iter->second);
currentW = currentW - W;
}
}
cout << totalValue << endl;
for(i=0; i < items.size(); i++) {
cout << items.at(i) << ' ';
}
输入文件:
最佳答案
我对您的解决方案有几点看法。
map
其中“key”是value per weight
的比率而“值”是 item's id
(项目在原始订单中的位置)。那么,如果两个项目具有相同的 value per weight
呢? ?在这种情况下, map 将只保留稍后出现的 map 。这可能会损坏您的解决方案。 Algorithm 2
在您的帖子中建议您需要按 item.value / item.weight
的降序对 (i) 进行排序(ii) 在平局的情况下,按 item.value
的降序排列.您在解决方案中没有考虑 (ii)。 #include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
#define myFile myFile
bool comp(const pair<double, pair<int, int>> &i1, pair<double, pair<int, int>> &i2) {
if(i1.first == i2.first) return (i1.second.second > i2.second.second);
return (i1.first > i2.first);
}
int main () {
string line;
int weightCapacity;
int numItems;
// double weight, value;
int weight, value;
map<double,int> set;
vector<int> w;
vector<int> v;
vector<int> knapsack;
int bestValue = 0;
int subsetValue = 0;
int subsetWeight = 0;
int i;
vector<int> items;
vector<pair<double, pair<int, int>>> wv_ratio;
ifstream myFile ("Heur0.txt");
// reading a text file
if (myFile.is_open()) {
cout << "Open" << endl;
}
else cout << "Unable to open file";
myFile >> weightCapacity;
myFile >> numItems;
// cout << weightCapacity << endl;
// cout << numItems << endl;
for (i = 0; i < numItems; i++) {
myFile >> weight;
myFile >> value;
//set[value/weight] = i;
wv_ratio.push_back(make_pair((value/(double) weight), make_pair(i, value)));
w.push_back(weight);
v.push_back(value);
//cout << weight << " " << value << endl;
}
sort(wv_ratio.begin(), wv_ratio.end(), comp);
int currentW = weightCapacity;
int totalValue = 0;
//for (auto iter = set.rbegin(); iter != set.rend(); ++iter) {
for (auto tmp = wv_ratio.begin(); tmp != wv_ratio.end(); ++tmp) {
pair<double, pair<int, int>> iter = (pair<double, pair<int, int>>) *(tmp);
//cout << iter->first << ": " << iter->second << endl;
cout << iter.second.first << ": " << iter.first << " " << iter.second.second << endl;
int W = w.at(iter.second.first);
if (W <= currentW) {
items.push_back(iter.second.first);
totalValue += v.at(iter.second.first);
currentW = currentW - W;
}
}
cout << totalValue << endl;
for(i=0; i < items.size(); i++) {
cout << items.at(i) << ' ';
}
return 0;
}
它产生这样的输出:
2889
782 585 765 741 628 862 618 727 770 394 386 71 535 844 941 789 610 874 777 412 771 319 358 391 607 276 235 223 329 216 790 747 631 700 541 842 229 878 344
显然它与您的预期不匹配。但是,我很确定我在这里分享的代码是根据
Algorithm 2
工作的。你的帖子。如果您认为您的预期输出绝对正确并且可以通过遵循
Algorithm 2
来实现,我建议您使用较小尺寸的输入进行调试。 .
关于c++ - 背包贪婪算法-我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66082947/
我是一名优秀的程序员,十分优秀!