1

I participated in a programming competition at my University. I solved all the questions except this one. Now I am practicing this question to improve my skills. But I can't figure out the algorithm. If there is any algorithm existing please update me. Or any similar algorithm is present then please tell me I will change it according to this question.

This is what I want to do.

  • The First line of input is the distance between two points.
  • After that, each subsequent line contains a pair of numbers indicating the length of cable and quantity of that cable. These cables are used to join the two points.
  • Input is terminated by 0 0

Output:

  • The output should contain a single integer representing the minimum number of joints possible to build the requested length of cableway. If no solution possible than print "No solution".

Sample Input

444 16 2 3 2 2 2 30 3 50 10 45 12 8 12 0 0 

Sample Output

10 
4
  • This question is better suited for cs.stackexchange.com and you can solve this with either dynamic programming or network flows. The former is more intuitive Commented Mar 27, 2020 at 5:18
  • 3
    en.wikipedia.org/wiki/Subset_sum_problem Commented Mar 27, 2020 at 5:18
  • to handle the max num per cable, I'd refer to knapsac problem. (where goal is 444, "branch value" is [sum, nb cable taken], and the max weight is applied on each cable type instead of the total nb of cable taken (as is "usually" done) Commented Mar 27, 2020 at 6:40
  • I'd use 9 lengths of 50 with knots around the 8 joints for strain-relief: you may be better off explicitly stating the sum of lengths needs to match exactly. Commented Mar 27, 2020 at 7:09

1 Answer 1

1

Thanks guys. I found a solution from "Perfect subset Sum" problem and then made a few changes in it. Here's the code.

#include <bits/stdc++.h> using namespace std; bool dp[100][100]; int sizeOfJoints = -1; void display(const vector<int>& v) { if (sizeOfJoints == -1) { sizeOfJoints = v.size() - 1; } else if (v.size()< sizeOfJoints) { sizeOfJoints = v.size() - 1; } } // A recursive function to print all subsets with the // help of dp[][]. Vector p[] stores current subset. void printSubsetsRec(int arr[], int i, int sum, vector<int>& p) { // If sum becomes 0 if (sum == 0) { display(p); return; } if(i<=0 || sum<0) return; // If given sum can be achieved after ignoring // current element. if (dp[i-1][sum]) { // Create a new vector to store path //vector<int> b = p; printSubsetsRec(arr, i-1, sum, p); } // If given sum can be achieved after considering // current element. if (sum >= arr[i-1] && dp[i-1][sum-arr[i-1]]) { p.push_back(arr[i-1]); printSubsetsRec(arr, i-1, sum-arr[i-1], p); p.pop_back(); } } // all subsets of arr[0..n-1] with sum 0. void printAllSubsets(int arr[], int n, int sum) { if (n == 0 || sum < 0) return; // If sum is 0, then answer is true for (int i = 0; i <= n; i++) dp[i][0] = true; // If sum is not 0 and set is empty, then answer is false for (int i = 1; i <= sum; i++) dp[0][i] = false; // Fill the subset table in botton up manner for (int i = 1; i <= n; i++) { for (int j = 1; j <= sum; j++) { if(j<arr[i-1]) dp[i][j] = dp[i-1][j]; if (j >= arr[i-1]) dp[i][j] = dp[i-1][j] || dp[i - 1][j-arr[i-1]]; } } if (dp[n][sum] == false) { return; } // Now recursively traverse dp[][] to find all // paths from dp[n-1][sum] vector<int> p; printSubsetsRec(arr, n, sum, p); } // Driver code int main() { int input[2000]; int inputIndex = 0; int i = 0; int distance = 0; cout<< "Enter Input: " <<endl; cin>> distance; while(true) { int temp1 = 0; int temp2 = 0; cin>> temp1; cin>> temp2; if (temp1 == 0 && temp2 == 0) { break; } for (i = 0; i < temp2; i++) input[inputIndex++] = temp1; } cout<< "Processing output. Please wait: " <<endl; printAllSubsets(input, inputIndex, distance); if(sizeOfJoints != -1) cout<<sizeOfJoints; else cout<<"No Solution Possible"; return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.