-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllpair.c
143 lines (72 loc) · 1.28 KB
/
Allpair.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include<stdio.h>
int main(){
int c[20][20]={0},A[20][20],D[20][20],n,e,u,v,w,i,j,k,no_path,a[10];
printf("Enter no of vertices\t");
scanf("%d",&n);
printf("Enter no of edges\t");
scanf("%d",&e);
for(i=0;i<e;i++){
printf("Enter nodes and weight\t");
scanf("%d%d%d",&u,&v,&w);
c[u-1][v-1]=w;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(c[i][j]==0)
D[i][j]=-1;
else
D[i][j]=i+1;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i!=j && c[i][j]==0)
c[i][j]=999;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
A[i][j]=c[i][j];
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(A[i][j]>(A[i][k]+A[k][j]))
{
A[i][j]=A[i][k]+A[k][j];
D[i][j]=k+1;
}
printf("Length matrix is\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d\t",A[i][j]);
}
printf("\n");
}
printf("Decision matrix is\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%d\t",D[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(D[i][j]!=-1)
D[i][j]-=1;
printf("Enter no of paths you want\t");
scanf("%d",&no_path);
for(i=0;i<no_path;i++){
printf("Enter node to find path\t");
scanf("%d%d",&u,&v);
k=0;
u--; v--;
j=v;
do{
a[k++]=j+1;
j=D[u][j];
}while(j!=u);
a[k]=u+1;
printf("Cost is %d\t",A[u][v]);
printf("Path is : ");
for(j=k;j>0;j--)
printf("%d-",a[j]);
printf("%d\n",a[0]);
}
return 0;
}