-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack.cpp
48 lines (44 loc) · 993 Bytes
/
knapsack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* knapsack.cpp
*
* Created on: Jul 11, 2016
* Author: Mohamed
*/
#include<bits/stdc++.h>
using namespace std;
struct item {
int value;
int weight;
};
bool cmp3(struct item a, struct item b) {
double r1 = (double) a.value / a.weight;
double r2 = (double) b.value / b.weight;
return r1 > r2;
}
double fraction_knapsack(int cap, item a[], int n) {
sort(a, a + n, cmp3);
double x = 0;
int i = 0;
double w, v;
while (cap > 0) {
w = min(cap, a[i].weight);
v = w * a[i].value / a[i].weight;
x += v;
cap -= w;
i++;
}
return x;
}
int optimal_weight(int W, const vector<int> &w) {
//write your code here
vector<vector<int> > dp(w.size() + 1, vector<int>(W + 1,0));
for (int i = 1; i <= w.size(); ++i) {
for (int j = 1; j <= W; ++j) {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (j >= w[i - 1]) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + w[i - 1]);
}
}
}
return dp[w.size()][W];
}