简体   繁体   English

分数背包算法分段错误

[英]Fractional Knapsack Algorithm segmentation fault

I am trying to solve the fractional knapsack problem by writing a function get_optimal_value which returns the total optimal value given the capacity of knapsack and weights and values of items.我试图通过编写 function get_optimal_value 来解决分数背包问题,它返回给定背包容量以及物品重量和值的总最优值。 Over here in the function get_optimal_value, i am running into a segmentation fault problem on v_per_w[i] = values[i]/weights[i];在 function get_optimal_value 中,我在v_per_w[i] = values[i]/weights[i];上遇到了分段错误问题。 and i am not sure what is going on as i can't see where i have accessed foreign memory.而且我不确定发生了什么,因为我看不到我在哪里访问了外国 memory。 This is my program.这是我的程序。 Help would be appreciated thanks:D帮助将不胜感激谢谢:D

#include <iostream>
#include <vector>
#include <algorithm>

using std::vector;

double get_optimal_value(int capacity, vector<int> weights, vector<int> values) {
  double value = 0.0;
  vector<double> v_per_w = {};
  for (int i =0; i<weights.size(); ++i){
    v_per_w[i] = values[i]/weights[i]; //calculate v/w for the vector
  }

  //as long as capacity is not full
  while (capacity !=0){
    auto it = std::max_element(v_per_w.begin(), v_per_w.end()); //find max element in v/w
    auto it2 = find(v_per_w.begin(), v_per_w.end(), *it); // find the index of that max element in v/w
    int largest_index = it2 - v_per_w.begin(); 
    if (capacity>=weights[largest_index]){ 
      value+= values[largest_index]; //if capacity is more than the weight of that max v/w, add it all in
    }
    else{
      value += v_per_w[largest_index] * capacity; //else add whatever weight you can of that max v/w
    }
    values.erase(values.begin(), values.begin()+largest_index-1); //remove that item from all the vectors
    weights.erase(weights.begin(), weights.begin()+largest_index-1);
    v_per_w.erase(v_per_w.begin(), v_per_w.begin()+largest_index-1);
  }

  return value;
}

int main() {
  int n;
  int capacity;
  std::cin >> n >> capacity;
  vector<int> values(n);
  vector<int> weights(n);
  for (int i = 0; i < n; i++) {
    std::cin >> values[i] >> weights[i];
  }

  double optimal_value = get_optimal_value(capacity, weights, values);

  std::cout.precision(10);
  std::cout << optimal_value << std::endl;
  return 0;
}
vector<double> v_per_w = {};

This creates a new vector , which is completely empty.这会创建一个完全为空的新vector It has no values.它没有价值。 Immediately afterwards:紧接着:

v_per_w[i] = values[i]/weights[i]; //calculate v/w for the vector

This attempts to change the existing values of v_per_w .这会尝试更改v_per_w的现有值。 However, since v_per_w is completely empty it has no values;但是,由于v_per_w完全为空,它没有任何值; this is a near-certain guarantee to produce the type of a crash you've observed.这是产生您观察到的崩溃类型的几乎肯定的保证。 v_per_w[something] does not add new values to a vector. v_per_w[something]不会向向量添加新值。 It modifies/accesses existing values in the vector, which must exist already.它修改/访问向量中必须已经存在的现有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM