forked from thisisshub/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractionlKnapsack.cpp
71 lines (58 loc) · 1.73 KB
/
FractionlKnapsack.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
#include<iostream>
#include<algorithm>
#include<utility>
#include<vector>
//maximum total weight using value in the knapsack with fractional items
using namespace std;
bool sortinrev(const pair<int,int> &a,const pair<int,int> &b)
{
return (a.second > b.second); //sort in value comparisons
}
int main(){
vector< pair <int,int> > a;
int x,y,n,w,v;
int cw = 0;
double finval = 0.0;
cout<<"\nenter the number of items:";//take list of items
cin>>n;
for(int i=0;i<n;i++){
cout<<"enter item weight "<<i+1<<" :\n";
cin>>x;
cout<<"enter item value "<<i+1<<" :\n";
cin>>y;
a.push_back(make_pair(x,y));
}
cout<<endl;
sort(a.begin(), a.end(),sortinrev);
cout<<"Item weight Item value"<<endl;
for (int i=0; i<n; i++)
{
// 1st and 2nd element of pair respectively
cout << a[i].first <<"\t\t"<< a[i].second << endl;
}
cout<<"\nenter the size of the knapsack: ";
cin>>w;
int j=0;
for (int i = 0; i < n; i++) {
// If adding Item won't overflow, add it completely
if (cw + a[i].first <= w)
{
cw += a[i].first;
j+=a[i].first;
finval += a[i].second;
cout<<"item number with weight "<<a[i].first<<" entered"<<endl;
}
// If we can't add current Item, add fractional part of it
else {
int r = w - cw;
double z;
z=a[i].first * ((double)r/(double)a[i].first);
j+=z;//max weight
finval += a[i].second * ((double)r/(double)a[i].second);//max val/profit
cout<<"item number with "<<z<<" of weight "<<a[i].first<<" entered"<<endl;
break;
}
}
cout << "\n profit is - " << finval;
cout << "\n max weight is - " << j;
}