-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumCostPathFinderUsingAdjacentMatrix.c
63 lines (54 loc) · 1.34 KB
/
MinimumCostPathFinderUsingAdjacentMatrix.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
int main() {
int n, r, c, i, j;
printf("Enter no of vertices:");
scanf("%d", &n);
int a[n][n];
printf("enter cost adjacency matrix of a graph with %d vertices\n", n);
// Reading adjacency matrix
for(int i=1;i <=n;i++) {
for(int j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter rows and columns of path array");
scanf("%d%d",&r,&c);
int temp[r][c];
// Reading paths
printf("Enter paths");
for(int i=1;i<=r;i++) {
for(int j=1;j<=c;j++) {
scanf("%d", &temp[i][j]);
}
}
int mincost=1000000;
int minr=0;
for(int i=1;i<=r;i++) {
int cost = 0;
int flag=1;
for(int j=1;j<c;j++) {
if(a[temp[i][j]][temp[i][j+1]]!=0) {
cost = cost + a[temp[i][j]][temp[i][j+1]];
} else {
flag = 0;
break;
}
}
if (flag) {
if (mincost >= cost) {
mincost = cost;
minr = i;
}
}
}
if (minr == 0) {
printf("No valid path found\n");
} else {
printf("Minimum cost path: ");
for(int i=1;i<=c;i++) {
printf("%d->", temp[minr][i]);
}
printf("\nMinimum cost: %d\n", mincost);
}
return 0;
}