-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha3.c
63 lines (51 loc) · 862 Bytes
/
a3.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
//Bellman-Ford
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[10][10],n,s;
printf("\nEnter Number of Vertices:");
scanf("%d",&n);
printf("\nEnter the Adjacency Matrix:-\n");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(i!=j && a[i][j]==0)
a[i][j]=999;
}
int d[10];
for(int i=0;i<n;i++)
d[i]=999;
int p[10]={-1};
printf("\nEnter the Source Node: ");
scanf("%d",&s);
d[s]=0;
for(int k=1;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(d[j]>d[i]+a[i][j])
{
d[j]=d[i]+a[i][j];
p[j]=i;
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(d[j]>d[i]+a[i][j])
{
printf("\nThe Graph contains Negative Cycles!\n");
exit(0);
}
}
}
for(int i=0;i<n;i++)
printf("Distance to Node %d = %d\n",i,d[i]);
}