- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Connect n ropes with minimum costn
There are N ropes of given lengths. We have to connect with them. The cost of connecting one rope with other is the sum of their lengths. Our goal is to connect the N ropes with minimum cost.
This problem can be solved using a heap tree. We will create min heap to insert all different lengths first, then remove minimum and second minimum item from min heap, connect them and again insert into the heap tree. When the heap will hold only one element, we can stop the process and get the connected rope with minimum costs.
Input and Output
Input: The lengths of the ropes: {4, 3, 2, 6, 5, 7, 12} Output: Total minimum cost: 103
Algorithm
findMinCost(array, n)
Input − List of rope lengths, number of entries in the list.
Output − Minimum cost to cut.
Begin minCost := 0 fill priority queue with the array elements, (greater value is higher priority) while queue is not empty, do item1 := get item from queue and delete from queue item2 := get item from queue and delete from queue minCost := minCost + item1 + item2 add (item1 + item2) into the queue done return minCost End
Example
#include<iostream> #include<queue> #include<vector> using namespace std; int findMinimumCost(int arr[], int n) { //priority queue is set as whose value is bigger, have higher priority priority_queue< int, vector<int>, greater<int>>queue(arr, arr+n); int minCost = 0; while (queue.size() > 1) { //when queue has more than one element int item1 = queue.top(); //item1 is the shortest element queue.pop(); int item2 = queue.top(); //item2 is bigger than item1 but shorter then other queue.pop(); minCost += item1 + item2; //connect ropes and add them to the queue queue.push(item1 + item2); } return minCost; } int main() { int ropeLength[] = {4, 3, 2, 6, 5, 7, 12}; int n = 7; cout << "Total minimum cost: " << findMinimumCost(ropeLength, n); }
Output
Total minimum cost: 103
- Related Articles
- Minimum Cost to Connect Sticks in C++
- Connecting Cities With Minimum Cost in Python\n
- Program to find minimum cost to connect all points in Python
- Program to find minimum cost to connect each Cartesian coordinates in C++
- Minimum Cost Polygon Triangulation
- Minimum Cost For Tickets in C++
- C Program for Minimum Cost Path
- Rohit took with him some nylon ropes, when he was going for rock climbing. Can you tell why he selected nylon ropes instead of ropes made up of cotton or jute?
- Minimum Cost to Merge Stones in C++
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed in C++
- Minimum Cost Tree From Leaf Values in Python
- Minimum Cost to Hire K Workers in C++
- Program to find minimum cost to paint fences with k different colors in Python
- Using JCo to connect SAP with Android
- WiFi with Arduino – Connect to a Network

Advertisements