-
Notifications
You must be signed in to change notification settings - Fork 1
/
MinCost.java
37 lines (32 loc) · 1.13 KB
/
MinCost.java
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
34
35
36
37
public class MinCost {
/**
* Author:- Gaurav Shrivastava
*/
// This function returns the smallest possible cost to
// reach station N-1 from station 0.
public static int MinimumCost(int cost[][],int N){
// dist[i] stores minimum cost to reach station i
// from station 0.
int dist[] = new int[N];
for (int i=0; i<N; i++)
dist[i] = Integer.MAX_VALUE;
dist[0] = 0;
// Go through every station and check if using it
// as an intermediate station gives better path
for (int i=0; i<N; i++)
for (int j=i+1; j<N; j++)
if (dist[j] > dist[i] + cost[i][j])
dist[j] = dist[i] + cost[i][j];
return dist[N-1];
}
public static void main(String[] args) {
int cost[][] = { {0, 15, 80, 90},
{Integer.MAX_VALUE, 0, 40, 50},
{Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 70},
{Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0}
};
System.out.print(cost[0].length);
System.out.println( "The Minimum cost to reach station "
+ cost[0].length + " is " + MinimumCost(cost,cost.length));
}
}