Skip to content

Instantly share code, notes, and snippets.

@aadimator
Created July 25, 2016 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadimator/35634e7e3ba771e34a77ab371cbbb697 to your computer and use it in GitHub Desktop.
Save aadimator/35634e7e3ba771e34a77ab371cbbb697 to your computer and use it in GitHub Desktop.
Fractional Knapsack
#include <iostream>
#include <vector>
using std::vector;
int get_max_index (vector<int> weights, vector<int> values) {
int max_i = 0;
double max = 0;
for (int i = 0; i < weights.size(); i++) {
if (weights[i] != 0 && (double)values[i]/weights[i] > max) {
max = (double)values[i]/weights[i];
max_i = i;
}
}
return max_i;
}
double get_optimal_value(int capacity, vector<int> weights, vector<int> values) {
double value = 0.0;
for (int i = 0; i < weights.size(); i++) {
if (capacity == 0) return value;
int index = get_max_index(weights, values);
int a = std::min(capacity, weights[index]);
value += a * (double)values[index]/weights[index];
weights[index] -= a;
capacity -= a;
}
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment