Advertisement
skb50bd

Fractional Knapsack Problem

Oct 31st, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <algorithm>
  6. #include <cstdio>
  7. #define SIZE 100
  8. #define TAB 8
  9. using namespace std;
  10.  
  11. struct item {
  12.     int ID;
  13.     int weight;
  14.     int value;
  15.     double wm;
  16.     double taken;
  17. };
  18.  
  19.  
  20. bool comp(struct item X, struct item Y) {
  21.     return X.wm > Y.wm;
  22. }
  23.  
  24.  
  25. double fillUpKnapsack(struct item A[], int numberOfItems, int W) {
  26.     int remainingSpace = W;
  27.     double cost = 0.0;
  28.  
  29.     for (int i = 0; i < numberOfItems && remainingSpace; i++) {
  30.         if (A[i].weight <= remainingSpace) {
  31.             cost += A[i].value;
  32.             A[i].taken = 1.0;
  33.             remainingSpace -= A[i].weight;
  34.         }
  35.         else {
  36.             cost += 1.0 * A[i].value / A[i].weight * remainingSpace;
  37.             A[i].taken = 1.0 * remainingSpace / A[i].weight;
  38.             remainingSpace = 0;
  39.         }
  40.     }
  41.     return cost;
  42. }
  43.  
  44.  
  45. void printItems(struct item A[], int numberOfItems) {
  46.     cout << setw(TAB) << "ID";
  47.     cout << setw(TAB) << "Value";
  48.     cout << setw(TAB) << "Weight";
  49.     cout << setw(TAB) << "V/W";
  50.     cout << setw(TAB) << "Taken";
  51.     cout << endl;
  52.     for (int i = 0; i < numberOfItems; i++) {
  53.         cout << setw(TAB) << A[i].ID;
  54.         cout << setw(TAB) << A[i].value;
  55.         cout << setw(TAB) << A[i].weight;
  56.         cout << setw(TAB) << A[i].wm;
  57.         cout << setw(TAB) << A[i].taken;
  58.         cout << endl;
  59.     }
  60.     cout << endl;
  61. }
  62.  
  63. int main() {
  64.     freopen("input.txt", "r", stdin);
  65.  
  66.     string line;
  67.     int W;
  68.  
  69.     cout << "Weight of Knapsack: ";
  70.     getline(cin, line);
  71.     stringstream(line) >> W;
  72.  
  73.     cout << "Items' Values and Weights: ";
  74.     getline(cin, line);
  75.  
  76.     istringstream iss(line);
  77.  
  78.     struct item A[SIZE];
  79.    
  80.     int numberOfItems;
  81.     for (numberOfItems = 0; iss >> line; numberOfItems++) {
  82.         A[numberOfItems].ID = numberOfItems + 1;
  83.         stringstream(line) >> A[numberOfItems].value;
  84.         iss >> line;
  85.         stringstream(line) >> A[numberOfItems].weight;
  86.         A[numberOfItems].wm = 1.0 * A[numberOfItems].value / A[numberOfItems].weight;
  87.         A[numberOfItems].taken = 0.0;
  88.     }
  89.  
  90.     sort(A, A + numberOfItems, comp);
  91.  
  92.     double cost = fillUpKnapsack(A, numberOfItems, W);
  93.     cout << "Maximum Cost: " << cost << endl;
  94.     printItems(A, numberOfItems);
  95.  
  96.     return 0;
  97. }
  98.  
  99.  
  100. /*
  101. SAMPLE INPUT:
  102. 5
  103. 10 4 15 5 6 1 14 2
  104. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement