1

The truck problem:

We need at least W1 weight of item1, and W2 weight of item2 for a minimum total value of gas spent.
Each truck carries w1 weight of item1, w2 weight of item2 and spends v of gas.

For example, input would be:

5 60 5    // W1, W2, number of records below
3 36 120  // w1, w2, v
10 25 129 // w1, w2, v
5 50 250  // w1, w2, v
1 45 130  // w1, w2, v
4 20 119  // w1, w2, v

And output should be:

249

I need to implement a function

int truck_knapsack(int W1, int W2, int w1[], int w2[], int v[], int n);

which returns the minimum total value of gas spent, where

  • n is number of records (trucks),
  • v[] values for gas spent,
  • w1[] weights of item1,
  • w2[] weights of item2,
  • W1 necessary weight of item1,
  • W2 necessary weight of item2.

I have found similar problem statements and solutions but I can't manage to derive a solution for this.

I am instructed by my teacher to solve this with 3d array bottom-up approach, but any solution would be very helpful.

Ely
  • 10,860
  • 4
  • 43
  • 64
Sara Cu
  • 11
  • 3
  • post what you have tried so far – pkfm Mar 31 '19 at 18:37
  • Hello Sara, the example input is not clear I am afraid. What is the first line (its comment is different than those of the other lines)? Also you say the output should be `249` in order for me to verify that I would need to understand the input (which leads to my previous question). Please try to explain the input example so that SOers can help you better – Ely Mar 31 '19 at 18:38
  • 1
    @Ely First line of input are total weight of item1 necessary, total weight of item2 necessary and number of inputs following, and each following input after that has weight of item1, weight of item2 and value of gas spent. – Sara Cu Mar 31 '19 at 18:43

1 Answers1

0

I believe that you could use the strategy described by this answer. Your problem is not a direct Knapsack formulation, but it can be transoformed into one.

Set TotalW1 = ∑w1i - W1 and TotalW2 = ∑w2i - W2. Now you can solve a Mulitple Constraints Knapsack problem

  • maximise ∑xivi
  • constraint 1: ∑xjw1j ≤ TotalW1 - W1
  • constraint 2: ∑xjw2j ≤ TotalW2 - W2

In order to get a solution for the minimisation problem statement for your question you simply take the complement of the Knapsack solution, i.e. the trucks which were not selected are the ones that minimise the gas consumption whilst carrying the minimum total weights expected.
According to the question the output should be the total value of the trucks that fulfil the weight conditions. Below is a recursive algorithm showing the example of your question:

#include <stdio.h>

int max(int a, int b)
{
  return (a > b ? a : b);
}

int truck_knapsack(int W1, int W2, int w1[], int w2[], int v[], int n)
{
  if (n == 0 || W1 == 0 || W2 == 0)
    return 0;

  if (w1[n - 1] > W1 || w2[n - 1] > W2)
    return truck_knapsack(W1, W2, w1, w2, v, n - 1);

  return max(
      v[n - 1] + truck_knapsack(W1 - w1[n - 1], W2 - w2[n - 1], w1, w2, v, n - 1),
      truck_knapsack(W1, W2, w1, w2, v, n - 1));
}

int main()
{
  int W1 = 5;
  int W2 = 60;
  int w1[] = {3, 10, 5, 4, 1};
  int w2[] = {36, 25, 50, 45, 20};
  int v[] = {120, 129, 250, 130, 119};
  int n = 5;

  // the problem statement is a variation of Knapsack problem
  // turn it into a classical Knapsack problem

  // total1 = sum of w1 weights - W1
  int total1 = 0;
  for (int i = 0; i < n; i++)
    total1 += w1[i];
  total1 -= W1;

  // total2 = sum of w2 weights - W2
  int total2 = 0;
  for (int i = 0; i < n; i++)
    total2 += w2[i];
  total2 -= W2;

  // the result of the Knapsack algorithm is the max
  // bounded by total1 and total2
  // the result of the original problem statement is sum_of_values - result
  int result = truck_knapsack(total1, total2, w1, w2, v, n);
  int sum_values = 0;
  for (int i = 0; i < n; i++)
    sum_values += v[i];
  printf("%d\n", sum_values - result);

  return 0;
}

The output is 249.

Ely
  • 10,860
  • 4
  • 43
  • 64