-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmin_cost_train.cpp
More file actions
33 lines (29 loc) · 843 Bytes
/
Copy pathmin_cost_train.cpp
File metadata and controls
33 lines (29 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// https://www.geeksforgeeks.org/find-the-minimum-cost-to-reach-a-destination-where-every-station-is-connected-in-one-direction/
#include<iostream>
#include<climits>
using namespace std;
#define INF INT_MAX
#define N 4
int minCost(int cost[][N])
{
int dist[N];
for (int i=0; i<N; i++)
dist[i] = INT_MAX;
dist[0]=0;
dist[1]= cost[0][1];
for (int i=2; i<N; i++)
for (int j=0; j<i; j++)
dist[i] = min(dist[j] + cost[j][i],dist[i]);
return dist[N-1];
}
int main()
{
int cost[N][N] = { {0, 15, 80, 90},
{INF, 0, 40, 50},
{INF, INF, 0, 70},
{INF, INF, INF, 0}
};
cout << "The Minimum cost to reach station "
<< N << " is " << minCost(cost);
return 0;
}