-
Notifications
You must be signed in to change notification settings - Fork 0
/
0-1Knapsack.cpp
85 lines (75 loc) · 2.01 KB
/
0-1Knapsack.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//This ONE!!
#include<bits/stdc++.h>
using namespace std;
vector<double> p;
vector<double> w;
vector<vector<double>> objList;
int n, M;
void printSoln(vector<double> x){
int sum=0;
cout<<"\nItems in Knapsack - \n"<<endl;
cout<<"Object\tFraction"<<endl;
for(int i=0; i<x.size(); i++){
cout<<fixed<<setprecision(0)<<objList[i][3]<<"\t";
cout<<fixed<<setprecision(2)<<x[i]<<endl;
}
for(int i=0; i<n; i++){
sum+=(x[i])*(objList[i][1]);
}
cout<<"Maximum profit: "<<sum<<endl;
}
void Knapsack(){
vector<double> x(n, 0);
int i;
int remCapacity = M;
for(i=0; i<n; i++){
if(w[i]>remCapacity)
break;
x[i] = 1;
remCapacity-=objList[i][2];
}
printSoln(x);
}
int main(){
int val, i, j, temp;
cout<<"Enter maximum weight"<<endl;
cin>>M;
cout<<"Enter number of objects"<<endl;
cin>>n;
cout<<"Enter profits of each object"<<endl;
for(i=0; i<n; i++){
cin>>val;
p.push_back(val);
}
cout<<"Enter weights of each object"<<endl;
for(i=0; i<n; i++){
cin>>val;
w.push_back(val);
}
cout<<"\nObject\tProfit\tWeight\tProfit/Weight Ratio"<<endl;
for(i=0; i<n; i++){
cout<<i+1<<"\t"<<p[i]<<"\t"<<w[i]<<"\t"<<p[i]/w[i]<<endl;
objList.push_back({double(p[i]/w[i]), p[i], w[i], double(i+1)});
}
sort(objList.rbegin(), objList.rend());
// for(i=0; i<n; i++){
// for(j=0; j<n; j++){
// if(p[i]/w[i] > p[j]/w[j]){
// temp=p[j];
// p[j]=p[i];
// p[i]=temp;
// temp=w[j];
// w[j]=w[i];
// w[i]=temp;
// }
// }
// }
cout<<"\nAfter Sorting..."<<endl;
cout<<"\nObject\tProfit\tWeight\tRatio"<<endl;
for(i=0; i<n; i++){
//cout<<i+1<<"\t"<<p[i]<<"\t"<<w[i]<<endl;
cout<<objList[i][3]<<"\t"<<objList[i][1]<<"\t"<<objList[i][2]<<"\t"<<objList[i][0]<<endl;
}
Knapsack();
return 0;
}