-
Notifications
You must be signed in to change notification settings - Fork 2
/
SHORT.TXT
74 lines (74 loc) · 1.11 KB
/
SHORT.TXT
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
//Floyd warshall shortest path using dyanamic programming
#include<stdio.h>
#include<conio.h>
#define inf 9999
int arr[5][5];
int cost[5][5];
int n;
void path()
{
int i,j,k,q,r;
for(k=0;k<n;k++)
{
printf("Intermediate vertex :%d",k+1);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=k && j!=k)
{
if(i!=j)
{
if(arr[i][j]>arr[i][k]+arr[k][j])
arr[i][j]==arr[i][k]+arr[k][j];
}
}
}
}
printf("\nShortest path : \n");
for(q=0;q<n;q++)
{
for(r=0;r<n;r++)
printf("\t%d",arr[q][r]);
printf("\n");
}
getch();
}
}
void creat()
{
int i,j;
printf("\nHow many nodes?");
scanf("%d",&n);
printf("\nEnter cost matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
arr[i][j]=0;
else
{
printf("\ncost [%d][%d]:",i+1,j+1);
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
arr[i][j]=inf;
else
arr[i][j]=cost[i][j];
}
}
}
printf("\n cost matrix:\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("\t%d",arr[i][j]);
printf("\n");
}
}
void main()
{
clrscr();
creat();
path();
}