
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find out the cost to travel all the given coordinates
Suppose, we are given n three-dimensional coordinates. The cost to travel from coordinate (a, b, c) to (x, y, z) is ∣ x − a∣ + ∣ y − b∣ + max(0, z − c). We start from the first coordinate, then visit all the coordinates at least once, and then return to the first coordinate. We have to find out the total cost of this whole trip. The coordinates are given to us in the array 'coords'.
So, if the input is like n = 3, coords = {{1, 1, 0}, {1, 3, 4}, {3, 2, 2}}, then the output will be 12.
To solve this, we will follow these steps −
Define one 2D array tpa. tpa[1, 0] := 0 for initialize i := 1, when i < 2n, update (increase i by 1), do: for initialize j := 0, when j < n, update (increase j by 1), do: if i mod 2 is same as 0, then: Ignore following part, skip to the next iteration for initialize t := 0, when t < n, update (increase t by 1), do: x := first value of coords[t] y := second value of coords[t] z := third value of coords[t] p := first value of coords[j] q := second value of coords[j] r := third value of coords[j] tpa[i OR (1 bitwise left shift t)][t] := minimum of (tpa[i|(1 bitwise left shift t)][t], tpa[i][j] + |x - p| + |y - q| + maximum of (0, z - r)) res := infinity for initialize i := 0, when i < n, update (increase i by 1), do: x := first value of coords[0] y := second value of coords[0] z := third value of coords[0] p := first value of coords[i] q := second value of coords[i] r := third value of coords[i] res := minimum of (res and tpa[2n - 1, i] + |x - p| + |y - q| + maximum of (0 and z - r)) return res
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; #define N 100 int solve(int n, vector<tuple<int,int,int>> coords){ vector<vector<int>> tpa(pow(2, n), vector<int>(n, INF)); tpa[1][0] = 0; for(int i = 1; i < pow(2,n); i++) { for(int j = 0; j < n; j++){ if(i % 2 == 0) continue; for(int t = 0; t < n; t++) { int x, y, z, p, q, r; tie(x, y, z) = coords[t]; tie(p, q, r) = coords[j]; tpa[i | (1 << t)][t] = min(tpa[i|(1 << t)][t], tpa[i][j] + abs(x - p) + abs(y - q) + max(0, z - r)); } } } int res = INF; for(int i = 0; i < n; i++) { int x, y, z, p, q, r; tie(x, y, z) = coords[0]; tie(p, q, r) = coords[i]; res = min(res, tpa[pow(2, n) - 1][i] + abs(x - p) + abs(y - q) + max(0, z - r)); } return res; } int main() { int n = 3; vector<tuple<int,int,int>> coords = {{1, 1, 0}, {1, 3, 4}, {3, 2, 2}}; cout<< solve(n, coords); return 0; }
Input
3, {{1, 1, 0}, {1, 3, 4}, {3, 2, 2}}
Output
12
- Related Articles
- C++ Program to find out the sum of shortest cost paths for all given triplets
- Program to Find Out the Minimum Cost to Purchase All in Python
- C++ program to find out the shortest cost path in a given graph for q queries
- Program to find minimum cost to connect each Cartesian coordinates in C++
- Program to find out the sum of minimum cost within a graph among all vertices in Python
- C++ program to find out the center coordinates and the height of a building
- Program to Find Out the Cost after Finding k Unique Subsequences From a Given String in C++
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to find out the minimum path to deliver all letters in Python
- Program to find minimum cost to connect all points in Python
- Program to find out the minimal cost so that the citizens have access to a market in Python
- Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Python Program to find out the determinant of a given special matrix
- Program to find out the value of a given equation in Python

Advertisements