-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP25_1_fractional_knapsack.cpp
70 lines (67 loc) · 1.96 KB
/
P25_1_fractional_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<vector>
#include<bits/stdc++.h>
using namespace std;
//Greedy about both profit and weight--> so we have calculated profit/weight ratio in it.
int main(){
int len;
double val,max_weight,max_profit=0.0;
cout<<"Enter the number of items"<<endl;
cin>>len;
cout<<"Enter the maximum weight of bag"<<endl;
cin>>max_weight;
double profit[len],weight[len],pwratio[len];
int index[len];
cout<<"Enter the values of profit:"<<endl;
for(int i=0;i<len;i++){
cin>>profit[i];
}
cout<<"Enter the values of corresponding weights:"<<endl;
for(int i=0;i<len;i++){
cin>>weight[i];
}
for(int i=0;i<len;i++){
pwratio[i]=(profit[i]/weight[i]);
index[i]=i;
}
// sort(pwratio.begin(), pwratio.end(), greater<int>());
for(int i=0;i<len-1;i++){
for(int j=0;j<len-i-1;j++){
if(pwratio[j]<pwratio[j+1]){
double temp=pwratio[j];
pwratio[j]=pwratio[j+1];
pwratio[j+1]=temp;
int temp1= index[j];
index[j]= index[j+1];
index[j+1]=temp1;
}
}
}
cout<<"The pw ratio is:"<<endl;
for(int i=0;i<len;i++){
cout<<pwratio[i]<<" ";
}
cout<<endl;
cout<<"The sorted indexes are:"<<endl;
for(int i=0;i<len;i++){
cout<<index[i]<<" ";
}
cout<<endl;
int i;
for( i=0;i<len;i++){
if(weight[index[i]]< max_weight && max_weight>0){
max_profit=max_profit+profit[index[i]];
max_weight=max_weight-weight[index[i]];
}
else{
break;
}
}
if(max_weight>0){
max_profit=max_profit+((profit[index[i]]*max_weight)/weight[index[i]]);
}
// cout<<"\n"<<i<<endl;
// cout<<"\nThe maximum weight is:"<<max_weight<<endl;
cout<<"The max profit is:"<<max_profit<<endl;
return 0;
}