- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the minimum cost to reach destination using a train

For this problem, there are N stops on a journey. The vehicle starts the journey from stop 0 to N-1. In a table, ticket costs for all pair of stations are given. We have to find the minimum cost to reach the destination with those given costs.

## Input and Output

Input: The cost matrix of the journey. 0 15 80 90 ∞ 0 40 50 ∞ ∞ 0 70 ∞ ∞ ∞ 0 Output: The minimum cost is 65. At first go to the destination 1 from 0. (cost 15), then 1 to 4 (cost 50). So total cost 65.

## Algorithm

findMinCost(cost)

**Input −** The cost matrix from each source to each destination.

**Output −** Find the minimum cost to reach a destination.

Begin define array costLoc, whose size is same as sumber of locations, fill costLoc with ∞. n := number of locations costLoc[0] := 0 for each source i to each destination j, do if costLoc[j] > costLoc[i] + cost[i, j], then costLoc[j] := costLoc[i] + cost[i, j] done return costLoc[n-1] End

## Example

#include<iostream> #define INF INT_MAX #define NODE 4 using namespace std; int cost[NODE][NODE] = { {0, 15, 80, 90}, {INF, 0, 40, 50}, {INF, INF, 0, 70}, {INF, INF, INF, 0} }; int findMinCost() { //find smallest possible cost to reach destination int costStation[NODE]; //store cost to reach any station from 0 for (int i=0; i<NODE; i++) costStation[i] = INF; //initially all are infinity costStation[0] = 0; //cost for station 0 is 0 as it is starting point for (int i=0; i<NODE; i++) for (int j=i+1; j<NODE; j++) if (costStation[j] > costStation[i] + cost[i][j]) //find intermediate station for min cost costStation[j] = costStation[i] + cost[i][j]; return costStation[NODE-1]; } int main() { cout << "The Minimum cost to reach station " << NODE << " is " << findMinCost() << endl; return 0; }

## Output

The Minimum cost to reach station 4 is 65

- Related Questions & Answers
- Minimum Initial Points to Reach Destination
- C++ program to find out the minimum amount of time needed to reach from source to destination station by train
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- Program to find minimum number of heights to be increased to reach destination in Python
- Program to find minimum cost to reach final index with at most k steps in python
- Find minimum steps required to reach the end of a matrix in C++
- Program to find minimum number of vertices to reach all nodes using Python
- Count number of ways to reach destination in a Maze in C++
- Program to find minimum cost to cut a stick in Python
- Program to find minimum jumps to reach home in Python
- Find minimum cost to buy all books in C++
- How to find the minimum number of jumps required to reach the end of the array using C#?
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find minimum cost to merge stones in Python
- C++ code to find minimum jump to reach home by frog

Advertisements