-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.cpp
71 lines (58 loc) · 1.16 KB
/
Dijkstra.cpp
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
#include<iostream>
using namespace std;
void Digkstra(int length[10][10],int n)
{
int a[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j] = length[i][j];
}
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if((a[i][k]+a[k][j]) < a[i][j])
{
length[i][j] = a[i][k] + a[k][j];
}
}
}
}
}
void display(int a[10][10],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int n;
cout << "Enter the size of graph : ";
cin >> n;
int length[10][10];
cout << endl << "Enter the " << n << "*" << n << " matrix : " << endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin >> length[i][j];
}
}
cout << endl << "Before Matrix : " << endl;
display(length,n);
Digkstra(length,n);
cout << endl << "After Digkstra's sort : " << endl;
display(length,n);
return 0;
}